Wednesday, April 2, 2008

Java Collections Lists (ArrayList LinkedList TreeList) and difference:

The list Interface extends Collection and it stores a sequence of elements as in list , all these elements can be easily accessed with their serial numbers or sequences and any element can be inserted to the list. But we have the situations when we get UnsupportedOperationException if the collection can’t be modified. List interface is implemented by three concrete classes

1) Array List

2) Linked List

3) Tree List

1. ArrayList

This class extends AbstractList and implements the List interface.This class can be said to be the improved version of the arrays as in arrays we have the limitation of the fixed size which has to be known and defined well in advance and the n nothing much can be done with respect to the increase and decrease in the size of the array , so we have got the extended class from collections framework whch gives us the flexibility of having the variable array structure which can be dynamically increased or reduced as per the programmers need. An ArrayList is a variable-length array of object references.

ArrayList has three constructors shown here:

a) Array list ():
This constructor builds an empty array list.

b) ArrayList(Collection c):
This constructor builds an array list that is initialized with the elements of the collection c.

c) ArrayList (int capacity):
This constructor builds an array list that has the specified initial capacity. The capacity grows automatically as elements are added to an array list.

import java.util.ArrayList;
import java.util.List;

public class CreateArrayList {

public static void main(String args[]) {
List list = new ArrayList();
list.add("1");
list.add("2");
list.add("3");
}
}The above code will create an ArrayList object and store three String objects in the ArrayList object.

import java.util.ArrayList;
import java.util.List;


public class DisplayArrayList {

public static void main(String args[]) {
List list = new ArrayList();
list.add("1");
list.add("2");
list.add("3");

for(int i = 0; i < 3; i++) {
System.out.println(list.get(i));
}
}
}Output of the above code

1
2
3
Below code is an example where the arraylist contains many Employee objects. Try this code a see how it works.

import java.util.ArrayList;
import java.util.List;

public class Employees {

public static void main(String args[]) {
List employeesList = new ArrayList();
employeesList.add(new Employee("Sam", 100000));
employeesList.add(new Employee("Rohan", 200000));
employeesList.add(new Employee("John", 300000));
employeesList.add(new Employee("Willey", 400000));

int size = employeesList.size();
for (int i = 0; i < size; i++) {
Employee employee = (Employee) employeesList.get(i);
System.out.println(employee.toString());
}
}
}

class Employee {

private String name;

private long sal;

public Employee(String name, long sal) {
this.name = name;
this.sal = sal;
}

public String getName() {
return name;
}

public long getSal() {
return sal;
}

public String toString() {

return "Name : " + getName() + "\n" + "Salary of " + getName() + ": "
+ getSal();
}
}


2. LinkedList :

LinkedList Class: This Class extends AbsractSequentialList and implements the List Interface. It as it defines automatically by it’s term provides a Linked-list structure.
It has two constructors which are mentioned below:

LinkedList ( ) LinkedList(Collection c)

LinkedList ( ): This constructor builds an empty Linked List. LinkedList(Collection c): This builds a linked list which is initialized with elements of collection .

Try the below code

import java.util.LinkedList;
import java.util.List;


public class LinkedListExample {

public static void main(String args[]) {

List linkedList = new LinkedList();
linkedList.add("1");
linkedList.add("2");
linkedList.add("3");
}
}
3. TreeList :

public class TreeList extends AbstractList (Code)
A List implementation that is optimised for fast insertions and removals at any index in the list.
This list implementation utilises a tree structure internally to ensure that all insertions and removals are O(log n). This provides much faster performance than both an ArrayList and a LinkedList where elements are inserted and removed repeatedly from anywhere in the list.

The following relative performance statistics are indicative of this class:

get add insert iterate remove
TreeList 3 5 1 2 1
ArrayList 1 1 40 1 40
LinkedList 5800 1 350 2 325

ArrayList is a good general purpose list implementation. It is faster than TreeList for most operations except inserting and removing in the middle of the list. ArrayList also uses less memory as TreeList uses one object per entry.
LinkedList is rarely a good choice of implementation. TreeList is almost always a good replacement for it, although it does use sligtly more memory.

Difference between LinkedList ArrayList TreeList:
There are some differences between an java.util.ArrayList and java.util.LinkedList. These differences are not so important for any small applications that processes small amount of data. Then they can very important for any small application that processes huge amount of data. These do affect the performance of a application if not analyzed properly.

An empty LinkedList takes 2-3 times less memory than an empty ArrayList. This becomes important if you plan to keep an adjacency list of nodes for each node in the graph (graph can be like a TreeSet. Tree is a type of graph) and you know beforehand that the nodes will have at most one or two adjacent nodes and the total number of nodes in the graph can be quite large.


Although both ArrayList and LinkedList allow insertion/deletion in the middle through similar operations (ie; by invoking add(int index) or remove(index)), these operations do not offer you the advantage of O(1) insertion/deletion for a LinkedList. For that, you must work through a ListIterator.


The important thing to remember when comparing LinkedList and ArrayList is that linked lists are more faster when inserting and removing at random locations in the list multiple times. If you want to add to the end of the list then an ArrayList would be the best choose.

I will try to supply an answer for the LinkedList vs ArrayList issue. The first thing to do is to look at what interfaces these two implement. This might hint us at their purpose, as Sun usually bound purposes into interfaces.

// lang java
public class ArrayList
extends AbstractList
implements List, RandomAccess, Cloneable, Serializable

public class LinkedList
extends AbstractSequentialList
implements List, Queue, Cloneable, Serializable
We can ignore Cloneable and Serializable as all of the JCF implement those. It’s also quite obvious that both of them implement the List interface, as they need to provide list functionability (backwards iteration, sub listing, etc).
Now for the differences: ArrayList is implementing RandomAccess while LinkedList implementing Queue. You could already tell something about the usage of the two classes.
ArrayList:
Now for some implementation notes. The ArrayList is actually encapsulating an actualy Array, an Object[]. When you instanciate ArrayList, an array is created, and when you add values into it, the array changes its size accordingly. This gives you strengths and weaknesses:
• Fast Random Access
You can perform random access without fearing for performence. Calling get(int) will just access the underlying array.
• Adding values might be slow When you don’t know the amount of values the array will contain when you create it, a lot of shifting is going to be done in the memory space when the ArrayList manipulates its internal array.
• Slow manipulation When you’ll want to add a value randomly inside the array, between two already existing values, the array will have to start moving all the values one spot to the right in order to let that happen.
LinkedList
The LinkedList is implemented using nodes linked to each other. Each node contains a previous node link, next node link, and value, which contains the actual data. When new data is inserted, a node is inserted and the links of the surrounding nodes are updated accordingly. When one is removed, the same happens - The surrounding nodes are changing their links and the deleted node is garbage collected. This, as well, gives strengths and weaknesses:
• Fast manipulation As you’d expect, adding and removing new data anywhere in the list is instantanious. Change two links, and you have a new value anywhere you want it.
• No random access Even though the get(int) is still there, it now just iterates the list until it reaches the index you specified. It has some optimizations in order to do that, but that’s basically it.
Conclusion:
ArrayList is very useful when a well defined set of data is needed in a List interface as opposed to an array. It can be dynamically changed, but try not to do so frequently throughout the life of the application. LinkedList is there for you to do just that: Manipulating it is very easy, and as long as its used for iteration purposes only and not for random accessing, it’s the best solution. Further, if you need random accessing from time to time, I suggest toArray for that specific moment.
Another point I didn’t raise here is the Queue issue. LinkedList implements extended abilities to the normal List interface which allows it to add and remove elements from its beginning and end. This makes the LinkedList perfect for Queue and Stack purposes - Although in Java 5 they already added a Stack class.

Set

Set:
A Set represents a mathematical set.
It is a Collection that, unlike List, does not allow duplicates.
There must not be two elements of a Set, say e1 and e2, such that e1.equals(e2).
The add method of Set returns false if you try to add a duplicate element.

import java.util.HashSet;
import java.util.Set;
public class MainClass {
public static void main(String[] a) {
Set set = new HashSet();
set.add("Hello");
if (set.add("Hello")) {
System.out.println("addition successful");
} else {
System.out.println("addition failed");
}
}
}

This tutorial is about Sets (java.util.Set interface). I assume that the audience have mathematical background of set theory.

Sets cannot contain duplicate elements and you cannot add elements at a certain position unlike List. The classes implementing the Set interface are:AbstractSet, HashSet, LinkedHashSet, TreeSet

Set has 3 general purpose implementations:

- HashSet
- TreeSet
- LinkedHashSet

HashSet does not offer any ordering of elements. So HashSet is not the right choice if you value oriented operations are required. For that, TreeSet is proffered. But it is true to say that HashSet is used more often than TreeSet because HashSet is more faster and the reason is obvious i.e no ordering of elements. LinkedHashSet is something in between HashSet and TreeSet. It is as fast as HashSet and is implements using HashSet and LinkedList. The benefit of LinkedHashSet is that it provides ordering feature but the cost of ordering is not even close to what provided by TreeSet.

HashSet

HashSet implement Cloneable, Collection, Serializable and Set interface.
Since HashSet implements Cloneable interface, it means that HashSet can use clone() method to make a copy (field to field) of its object. HashSet is also serializable, so it can be saved on disk and then retrieved later for use.
JobStateReasons and LinkedHashSet are direct subclasses of HashSet.

HashSet has 4 constructors:

HashSet()
HashSet(Collection c)
HashSet(int initialCapacity)
HashSet(int initialCapacity, float loadFactor)

If you use default HastSet constructor, it will create an empty HashSet of default initial capacity (16) and load factor (0.75).

Add() method of the HashSet add the object into the storage if it is not already present. Duplicates are not allowed as it’s a set.

Lets review the code below. We will just add unique values to a HashSet and will display the contents of HashSet.
Code:
String [] strArray = new String[10];

for loop strArray.length with i
strArray[i] = "Element" + i;

HashSet s = new HashSet();

for loop strArray.length with i{
if(!s.add(strArray[i]))
System.out.println("Duplicate found : " + strArray[i]);
}

System.out.println(s.size() + " distinct words detected : " + s );
}
Output:
Code:
10 distinct words detected : [Element6, Element7, Element1, Element9,
Element3, Element8, Element2, Element0, Element5, Element4]
As you know, HashSet does not allow duplicates, so lets try this in the following example.
Code:
String [] strArray = new String[10];
for loop strArray.length with i
strArray[i] = "Element" + i;

strArray[1] = "Element2";
strArray[7] = "Element3";
strArray[8] = "Element4";

HashSet s = new HashSet();

for loop strArray.length with i{
if(!s.add(strArray[i]))
System.out.println("Duplicate found : " + strArray[i]);
}

System.out.println(s.size() + " distinct words detected : " + s );
}
Output:
Code:
Duplicate found : Element2
Duplicate found : Element3
Duplicate found : Element4
7 distinct words detected : [Element6, Element9, Element3, Element2, Element0, Element5, Element4]
We tried to add duplicates into the HastSet which is not allowed. Thing to note is, that no exception will be raised in case of duplicates. Also note the order of entries in HashSet. The order of elements is not maintained.

Creating a HashSet through a Collection

HashSet(Collection c) constructor is used to store a collection as a HastSet. It is very useful in scenarios where you have some collection (it can be a List (e.g. Vector/ArrayList) and you want to store it into a HashSet. Lets review the following example where a HashSet is created using a Vector.
Code:
Vector vec = new Vector();
vec.add("String1");
vec.add("String2");
vec.add("String3");
HashSet hs = new HashSet(vec);
Writing/Retriving HashSet to disk

Since HashSet implements Serializable interface, its objects can be written and retrieved from the disk. In other words, we can save the state of a HashSet and can retrieve it when needed.
Code:
HashSet hs = new HashSet();
hs.add("String1");
hs.add("String2");
hs.add("String3");

FileOutputStream f_out = new FileOutputStream("C:\\hashset.data");
ObjectOutputStream obj_out = new ObjectOutputStream (f_out);

obj_out.writeObject (hs);
System.out.println("HashSet written on the disk.");

FileInputStream f_in = new FileInputStream("C:\\hashset.data");
ObjectInputStream obj_in = new ObjectInputStream (f_in);
HashSet hashset =
new HashSet((HashSet)obj_in.readObject());

System.out.println("HashSet fetched from the disk.");

Iterator it = hashset.iterator();

while(it.hasNext())
System.out.println(it.next().toString());
Union and Intersection Example

Following is an interesting example of union and intersection. I assume that you are familiar with basics of set theory and know what union and intersection are.
Code:
public static void main(String[] args) {
Set s1 = new HashSet();
s1.add("Australia");
s1.add("Sweden");
s1.add("Germany");

Set s2 = new HashSet();
s2.add("Sweden");
s2.add("France");

Set union = new TreeSet(s1);
union.addAll(s2);

print("union", union);

Set intersect = new TreeSet(s1);
intersect.retainAll(s2);

print("intersection", intersect);
}

protected static void print(String label, Collection c) {

System.out.println("--------------" + label + "--------------");

Iterator it = c.iterator();
while (it.hasNext()) {
System.out.println(it.next());
}
}
Output:
Code:
--------------union--------------
Australia
France
Germany
Sweden
--------------intersection--------------
Sweden
TreeSet

Treeset implements Cloneable, Collection, Serializable, Set and SortedSet interfaces. This class maintains order of the elements in it according to the natural order of the elements which was missing in the HashSet.

TreeSet is not synchronized so if a TreeSet is concurrently accessed by threads then thread modifying the contents must be synchronized externally.

TreeSet provides following constructors:

TreeSet()
TreeSet(Collection c)
TreeSet(Comparator c)
TreeSet(SortedSet s)
Lets dig deep into TreeSet using example. In the following example, we created an empty TreeSet using default constructor. Then we added some values into it.
Code:
TreeSet ts = new TreeSet();
ts.add("String1");
ts.add("String3");
ts.add("String2");

Iterator it = ts.iterator();

while(it.hasNext())
System.out.println(it.next().toString());
Output:
Code:
String1
String2
String3
We used iterator to print the contents of the TreeSet. Thing to note is the order of storage. Order of storage is ascending one.

We cannot insert element at a particular index and also cannot fetch element from a particular index. This is done in List. Also duplicates are not allowed in TreeSet. This is a property of Sets. If you try to add duplicate value, no exception will be thrown. Lets do this with an example:
Code:
TreeSet ts = new TreeSet();
ts.add("Australia");
ts.add("Germany");
ts.add("Zambia");
ts.add("Australia");
ts.add("Zambia");

Iterator it = ts.iterator();

while(it.hasNext())
System.out.println(it.next().toString());
Output:
Code:
Australia
Germany
Zambia
Output suggests two things. The objects are stored in sorted order and duplicates are not allowed.

Contents of TreeList can be cleared using clear() method. Following example creates a TreeSet from a Vector and then clears the contents.
Code:
Vector vec = new Vector();
vec.add("String3");
vec.add("String1");
vec.add("String3");

TreeSet ts = new TreeSet(vec);
Iterator it = ts.iterator();

while(it.hasNext())
System.out.println("TreeSet: " + it.next().toString());
ts.clear();
while(it.hasNext())
System.out.println("TreeSet_Cleared: " + it.next().toString());
Output:
Code:
TreeSet: String1
TreeSet: String3
Creating a TreeSet through a Collection

A TreeSet can be created using any collection. TreeSet(Collection c) is the constructor to use in such condition. Any collection for example: Vector, ArrayList, HashSet or any can be used to create a TreeSet. Following is an interesting example:
Code:
Vector vec = new Vector();
vec.add("String3");
vec.add("String1");
vec.add("String3");

Iterator it = vec.iterator();

while(it.hasNext())
System.out.println("Vector: " + it.next().toString());

TreeSet ts = new TreeSet(vec);
it = ts.iterator();

while(it.hasNext())
System.out.println("TreeSet: " + it.next().toString());
Output:
Code:
Vector: String3
Vector: String1
Vector: String3
TreeSet: String1
TreeSet: String3
Vector is List that can contain duplicates and its not ordered. In the example above, we created a TreeSet from a Vector that contained an unordered list of objects along with duplicates. Some of you might thing that its not possible to create a TreeSet from it but it is possible. The output of the above example shows that a TreeList is created from the Vector with all the duplicated removed. Furthermore, contents of newly created TreeList are ordered.

Can we create a TreeList from an exiting HashSet? Since HashSet is also a collection, so answer is yes. The following example first creates a HashSet, populates it and then creates a TreeSet using already created HashSet.
Code:
HashSet hs = new HashSet();
hs.add("String2");
hs.add("String3");
hs.add("String1");
hs.add("String2");

TreeSet ts = new TreeSet(hs);

Iterator it = ts.iterator();

while(it.hasNext())
System.out.println("TreeSet: " + it.next().toString());
Output:
Code:
TreeSet: String1
TreeSet: String2
TreeSet: String3
TreeSet with generics

Java 5 introduced generics which adds more power to Java. TreeSet stores objects in it, but that object can be of any type. It can be a String, Integer, Float or even a custom object. With generics, you can specify the type of objects that are allowed to be stored in a TreeSet. In the following example, I tried to store an integer into a Vector that is bounded by String.
Code:
TreeSet ts = new TreeSet();
ts.add(1);
ts.add(2);
ts.add(3);
ts.add("String2");
Output:
Code:
The method add(Integer) in the type TreeSet is not applicable for the arguments (String)
TreeSet Example

You now will have some understanding of TreeSet. Review the example below. It has some interesting lines of code.
Code:
SortedSet sortedSet = new TreeSet(Arrays
.asList("one two three four five six seven eight".split(" ")));
System.out.println(sortedSet);
Object low = sortedSet.first();
Object high = sortedSet.last();
System.out.println(low);
System.out.println(high);
Iterator it = sortedSet.iterator();

System.out.println("low: " + low);
System.out.println("high: " + high);
System.out.println(sortedSet.subSet(low, high));
System.out.println(sortedSet.headSet(high));
System.out.println(sortedSet.headSet(low));
System.out.println(sortedSet.tailSet(low));
System.out.println(sortedSet.tailSet(high));
Output:
Code:
[eight, five, four, one, seven, six, three, two]
eight
two
low: eight
high: two
[eight, five, four, one, seven, six, three]
[eight, five, four, one, seven, six, three]
[]
[eight, five, four, one, seven, six, three, two]
[two]
As we already know, ordering is important in TreeList. This is shown in the example below. We also see methods like first, last, headset, tailSet which produced interesting results.

Method headSet() returns a view of the portion of this set whose elements are strictly less than toElement.
Method tailSet() returns a view of the portion of this set whose elements are greater than or equal to fromElement.

Differences between HashSet and TreeSet

Many developer are confused about sets and ask when to use HashSet and when to use TreeSet. Keep following points in mind for deciding the right set.
- HashSet is much faster than TreeSet.
- HashSet offers no ordering guarantees.
- TreeSet is preffered when performing operations in the SortedSet.
- HashSet can be tuned by specifying the right initial capacity using the constructor HashSet(int initialCapacity).
- HashSet can also be tuned used load factor parameter. If you choose right value for this, it can boost the performance a bit but there there won't be a big difference.
- TreeSet has no tuning parameters.

LinkedHashSet

LinkedHashSet handles collisions by using hash-bucket approach to avoid linear probing and rehashing. It uses doubly-linked list to maintain the insertion order. LinkedHashSet can accept the null entry and it is not synchronized.

LinkedHashSet implements Cloneable, Collection, Serializable and Set interface. It’s a set to no duplicates are allowed. LinkedHashSet has following 4 constructors:

LinkedHashSet()
LinkedHashSet(Collection c)
LinkedHashSet(int initialCapacity)
LinkedHashSet(int initialCapacity, float loadFactor

Thing will become obvious when you start using LinkedHashSet. Lets take an example to make things easier to understand.
Code:
LinkedHashSet lhs = new LinkedHashSet();

lhs.add("String1");
lhs.add("String9");
lhs.add("String7");
lhs.add("String6");
lhs.add("String7");

Iterator it = lhs.iterator();

while(it.hasNext())
System.out.println("LinkedHashSet: " + it.next().toString());
Output:
Code:
LinkedHashSet: String1
LinkedHashSet: String9
LinkedHashSet: String7
LinkedHashSet: String6
We declared a LinkedHashSet and populated it. Then we used iterator to print the contents of LinkedHashSet. The output suggests that the order of insertion is maintained and duplicates are not allowed.

Set Example

Now we know what HashSet, TreeSet and LinkedHashSet are and when to use these. Lets review the following example that will use all of the 3 sets and will clear a lot of confusions:
Code:
package set;

import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;
import java.util.TreeSet;

public class HastSetTest {

static void fillSet(Set set) {
set.addAll(Arrays.asList("Zambia Australia Germany France".split(" ")));
}

public static void testSet(Set set) {
System.out.println(set.getClass().getName().replaceAll("\\w+\\.", ""));
fillSet(set);
fillSet(set);
fillSet(set);
System.out.println(set);
set.addAll(set);
set.add("Germany");
set.add("Germany");

System.out.println(set);
System.out.println("s.contains(\"Zambia\"): " + set.contains("Zambia"));
}

public static void main(String[] args) {
testSet(new HashSet());
testSet(new TreeSet());
testSet(new LinkedHashSet());
}
}
Output:
Code:
HashSet
[Germany, France, Zambia, Australia]
[Germany, France, Zambia, Australia]
s.contains("Zambia"): true
TreeSet
[Australia, France, Germany, Zambia]
[Australia, France, Germany, Zambia]
s.contains("Zambia"): true
LinkedHashSet
[Zambia, Australia, Germany, France]
[Zambia, Australia, Germany, France]
s.contains("Zambia"): true
The output suggests that no duplicate is added in any set. The order of elements is all of the 3 sets is different. LinkedList maintained the order of insertion. TreeSet maintained alphabetic order whereas HashSet does not kept any order. This is what we talked about above and this example confirms what we discussed.

Saturday, March 29, 2008

50 Rapidshare Premium Link Generator with out account (id and password)

HI,

All of us know Rapidshare is the very good to uploading files and it is very costly to get the premium accounts. I have tried from so long time, but I did not fount at least one password till now. Rapid share is highly protected. We did not download with out Premium account.

We have can download this premium files by the following websites. It will generate the download links. I have collected the following links. I think this will help you to download rapidshare links with out any premium account (id and password).

http://rapidshit.com/
http://3rs.dr.ag/
http://rapidshare.da.ru/
http://rapid4all.com/ http://www.rapidcheat.com/ http://www.rapiddownloader.info/ http://rapidcrack.fosi.eu.org/
http://elitehacks.blogspot.com/2007/07/rapidshare-premium-link-generator.html http://rapidhood.dr.ag/
http://rapidshare.premium.link.generator.googlepages.com/rs.htm http://www.nobakwaas.com/premium/ http://www.nopremium.org/ http://jsbi.blogspot.com/2007/06/free-rapidshare-premium-link-generator.html http://www.mymegalink.net/
http://rs43.com/
http://rapidshot666.dr.ag/
http://flinke-keule.dr.ag/
http://www.rapidsharepremium.org/index.php
http://www.yourpremiums.com/
http://megaez.com/
http://khongbiet.com/
http://mymegalink.net/
http://rapid-hook.com/
http://www.rapidl.com/
http://www.rapidrip.com/
http://computeraxes.com/rapid
http://rapid-hook.com/
http://www.rapidsharepremiumlinkgenerator.com/
http://rapidlysharing.com/
http://www.rapidshareplus.com/
http://www.fastlister.net/rs/
http://www.lcheat.com/rs7/
http://rapid-ripping.com/
http://x92.org/
http://www.rapidshare.co.in/
http://www.rapidleechers.com/
http://www.rapidshare-premium-downloader.com/
http://www.premium4me.com/rapid.html
http://www.premiumrapid.com/rapid.html
http://www.download-crazy.com/rapid
http://www.grab-w.net/
http://rapid.lin.co.il/
http://rapidlysharing.com/
http://rapidlysharing.com/
http://easyrapidsharepremium.blogspot.com/ http://www.t5f.net/
http://www.rapid-premium.wb.st/
http://www.premium4me.com/
http://www.premiumrapid.com/ http://adverbux.com/register.php?ref=20778
http://bux.to/?r=siongfai
http://www.bugonptc.com/index.php?ref=siongfai
http://inspiremarrow.com/pages/index.php?refid=siongfai
other 40 link genrators click

40 Websites Rapidshare Premium Link Generator with out account (id and password)

Rapid share id and passwords

hi all

Do not required to wait so long to to download rapidshare links.It is very easy to download rapidshare links with out wait lot of time.

he is the link that will take rapidshare link and download 10 links per day . it is very useful to download rapidsahre links with free accounts with out premium id and password.

http://www.rapidsharepremium.org/index.php

enjoy downloading by rapid share.

Monday, January 21, 2008

Vbscript Code Copy / Move Data MS Access To MS Excel

Hi

I have written vbscript code that can copy data from access to excel and also copy data from one excel sheet to other excel sheet (Copy data from sheet 1 to 2) in same workbook. i have created the Ms access database with some data and named as Db.mdb in D:/ (D: drive). i have created Microsoft excel with empty.

I have created one Excel Application object and named it as XLobj.  Open the existing excel workbook from D drive.  Enabled the excel application Visible property to true, to know the data is coped or not.

I have created ADODB Connection and ADODB Recordset objects and named it as objConn , resultSet respectively. i have opened the connection by setting Provider name Microsoft.Jet.OLEDB.4.0 and Data Source to my Ms access. By using Recordset object i have copied data from emp table to resultSet. i have changed the resultSet object to fist location.

By default excel Worksheets sheet-1 is Activate.  I have changed default Worksheets sheet-1 to Worksheets sheet-2 .By using Range i have set excel shell to A1. By using CopyFromRecordset method i have data from Recordset to Excel sheet.  The word Offset indicates the starting position of filed to copy data from access to excel.

I have also copy the data from Worksheets sheet-2 to Worksheets sheet-1 (copy data sheet 1 to sheet 2). First I have taken Worksheets sheet-2 and selected all the data from sheet. Then I have copied the selected data by using XLobj.Selection.Copy methode.

I have selected data in Worksheets sheet-1. i pasted data from Worksheets sheet-2 to Worksheets sheet-1. I have closed the Connection, Recordset objects and Save the workbook.

VB CODE FOR COPY DATA FROM ACCESS TO EXCEL  :

Dim XLobj,objConn,resultSet,TargetRange,xlSheet,xlSheet1

Set XLobj = CreateObject("Excel.Application")
XLobj.Workbooks.Open "D:\db.xls"
XLobj.Visible = True

Set objConn = CreateObject("ADODB.Connection")
set resultSet = CreateObject("ADODB.Recordset")
objConn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\Db.mdb"
Set resultSet = objConn.Execute("SELECT * FROM emp")
resultSet.MoveFirst

XLobj.Worksheets(2).Activate
Set TargetRange =XLobj.Range("A1")
TargetRange.Offset(1, 1).CopyFromRecordset resultSet

Set xlSheet = XLobj.Worksheets(2)
xlSheet.Select
XLobj.Selection.Copy

Set xlSheet1 = XLobj.Worksheets(1)
xlSheet1.Select
xlSheet1.Paste

resultSet.Close
objConn.Close
Set resultSet = Nothing
Set objConn = Nothing

XLobj.ActiveWorkbook.Save
XLobj.Quit

Google
 

blogger templates 3 columns | Techzilo