Querying SQL Server from Excel Using VBA

AnnRed

New Member
Joined
Sep 13, 2006
Messages
3
Hi,

PLEASE HELP ME
I'm new to vba. I need to connect to a sql server database and return that information into certain cells. I can return it into the first cell for one value but I'm having trouble looping...

(The column A contains usernames that I wish to search for in the Sql Server Db and The column G is where I want to return the timestamp if the username is found to have access to a certain application (This application value is stored in the cell G1) so I want to start entering values into G2 down to say G100)

Here is what I've come up with so far:

Code:
Sub Go()
For Each Acell In vRange
vRange = Range("A2:A6").Value
vVmsApp = Range("ITSecG1").Value

If IsEmpty(Acell) Then
Else
vSelect = "Select [Timestamp] from dbo.vVms where Username = '" & Acell & "' and Application = '" & vVmsApp & "';"
Set objData = objconn.Execute(vSelect)
vRange.Value = objData(0)
'Debug.Print objData(0)
End If
Next Acell
End Sub

I was using the following code that works, which let me return one value into one cell, but I want it to continue entering values down the column.

Code:
vUsername = Range("ITSec!A2").Value
vVmsApp = Range("ITSec!G1").Value

vSelect = "Select [Timestamp] from dbo.vVms where Username = '" & vUsername & "' and Application = '" & vVmsApp & "';"

objconn.Open vConn

Set objData = objconn.Execute(vSelect)
If objData.EOF Or objData.BOF Then
Else
Range("ITSec!G2").Value = objData(0)
End If

I hope some-one can help me cause I am so stuck!!! :( :cry:
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.

Forum statistics

Threads
1,214,992
Messages
6,122,631
Members
449,095
Latest member
bsb1122

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