Fetch data from database and put it in exel sheet by macro

PRIYARANJAN

New Member
Joined
Apr 20, 2011
Messages
5
Hi,
I have a requirement like below:
There is a database with following details:
username:scott,
password:tiger,
host name:xx.yy.zz.ppp,
port:abcd,
SID:zpz.

There is a employee table in the database.So we have to fetch the data from the table and put it into the xls sheet.

Here is the code:

Sub AnalyzeDBATables()

Dim cn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim col As Integer
Dim row As Integer

Set cn = New ADODB.Connection
Set rs = New ADODB.Recordset

cn.Open ( _
"User ID=scott" & _
";Password=tiger" & _
";Data Source= xxxxx" & _
";Provider=xxxx")

rs.Open "select * from employee", cn
col = 0

' First Row: names of columns
Do While col < rs.Fields.Count
Cells(1, col + 1) = rs.Fields(col).Name
col = col + 1
Loop

' Now actual data as fetched from select statement

row = 1
Do While Not rs.EOF
row = row + 1
col = 0

Do While col < rs.Fields.Count
Cells(row, col + 1) = rs.Fields(col).Value
col = col + 1
Loop
rs.MoveNext
Loop

End Sub



_______

I am using MAC OS and exel 2004.Here in the reference microsoft active x data object are not coming by defaults as in case of XP.

So while running the scripts the error is coming

Compilation Error:
User defined type not defined.

During debugging error is coming at :Set cn = New ADODB.Connection.



so please help me to sort out this..



Thanks &Regards,
priyaranjan
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
HI,

I am also facing the same issue, could anyone please guide me through to solve the problem and help us.

Thanx in advance,
Jibanendu:confused:
 
Upvote 0

Forum statistics

Threads
1,224,599
Messages
6,179,831
Members
452,946
Latest member
JoseDavid

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top