VBA code not retrieving any data

Stephen11

New Member
Joined
Jun 23, 2012
Messages
3
Hi, I am having trouble getting my VBA code to retrieve data from access. The code is as follows
Code:
Sub attempts() 
    Dim DBFullName As String 
    Dim TableName As String 
    Dim FieldName As String 
    Dim TargetRange As Range 
     
    Dim db As database 
    Dim rs As Recordset 
    Dim intColIndex As Integer 
     
    DBFullName = "C:\Users\Stephen\Desktop\Final database.accdb" 
    TableName = "SAMPLE" 
    FieldName = "ID" 
    MyCriteria = Sheets("test").Range("A2").Value 
    Set TargetRange = Range("A9") 
    Set TargetRange = TargetRange.Cells(1, 1) 
    Set db = OpenDatabase(DBFullName) 
    Set rs = db.OpenRecordset("SELECT * FROM " & TableName & _ 
    " WHERE " & FieldName & _ 
    " = 'MyCriteria'", dbReadOnly) 
     
     
     
     ' write field names
    For intColIndex = 0 To rs.Fields.Count - 1 
        TargetRange.Offset(0, intColIndex).Value = rs.Fields(intColIndex).Name 
    Next 
     ' write recordset
    TargetRange.Offset(1, 0).CopyFromRecordset rs 
    Set rs = Nothing 
    db.Close 
    Set db = Nothing 
     
End Sub

I am trying to tell the VBA that my criteria is a barcode in cell A2, sheet "test" of excel. However when I run the macro it only retrieves the database column headers and no data corresponding to the content of cell A2.
Anyone have any ideas where I am going wrong?
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
If ID is a text field try:
Code:
"SELECT * FROM " & TableName & " WHERE " & FieldName & " = '" & MyCriteria & "'"

If ID is a number field try:
Code:
"SELECT * FROM " & TableName & " WHERE " & FieldName & " = " & MyCriteria

Or use Microsoft Query with a cell parameter then no code required.
 
Upvote 0

Forum statistics

Threads
1,203,250
Messages
6,054,382
Members
444,721
Latest member
BAFRA77

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