View Single Post
  #3   Report Post  
Posted to microsoft.public.excel.programming
Mike Mike is offline
external usenet poster
 
Posts: 3,101
Default Pulling data from Access

Private Sub getDataFromAccess()
'Need to add reference to the
'Microsoft Active X Data Objects 2.0 or Higher
Dim cnn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim sSQL As String, strConn
Dim rowNumber As Long

'C:\PathToYourMdb\Ilsa.mdb (Change)
strConn = "Provider=Microsoft.Jet.OLEDB.4.0; " _
& "Data Source=C:\PathToYourMdb\Ilsa.mdb;Persist Security
Info=False"
'sSQL = "Replace with your query"
sSQL = "SELECT Field1, Field2 From TableName"

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

cnn.Open strConn
rs.Open sSQL, cnn, adOpenStatic, adLockOptimistic
rowNumber = 2 'Starting Row Number for data
Do While (Not rs.EOF)
Range("A" & rowNumber) = rs.Fields("Field1").Value
Range("B" & rowNumber) = rs.Fields("Field2").Value
rowNumber = rowNumber + 1
rs.MoveNext
Loop
rs.Close
Set rs = Nothing
cnn.Close
Set cnn = Nothing
End Sub

"Pam W." wrote:

First, I want to say how valuable this website is. I've gotten so much help
finding information from other posts.

I've used all this valuable information to create macros that have dazzled
my boss. Now, I'm afraid they think I can do anything. LOL. Let me know if
I'm in over my head--or how far over my head. (I am not familiar with
Access) They want to have an Access database and be able to query data using
Excel. Then they want to pull certain fields from one of the records chosen
by the user into an Excel worksheet. Once it's in this worksheet, it will
have other data added to various column. Then they want to be able to save
that record as a new record in the Access database.