MayFlowerNew
Programmer
Hi All,
I am a new to Excel, VB and using Access for excel through VBA.
I am trying to run a query in a VB macro and return the result to the excel sheet.
I dont get any compiler error but I dont get any result too..
please help..
Here is the code
Public Const DEFAULT_PATH As String = "C:\AccessDatabase"
Public Const DEFAULT_DB As String = "db1.mdb"
Public Sub Aut
pen()
On Error Resume Next
Dim rs As Recordset
Dim qy As QueryDef
Dim Row As Integer
Dim Row As Integer
db_name = DEFAULT_PATH & "\" & DEFAULT_DB
If (Len(db_name) > 0) Then
Set g_db = OpenDatabase(db_name)
Else
MsgBox "No DB open!"
End
End If
Set qy = g_db.CreateQueryDef("", "Select * from Table1")
Set rs = qy.OpenRecordset
Row = 1
With rs
Do While Not .EOF
Worksheets("Sheet1").Cells(Row, 1) = rs.Fields(0).Name
Worksheets("Sheet1").Cells(Row, 1) = rs.Fields(1).Name
Row = Row + 1
.MoveNext
End If
Loop
End With
rs.Close
ThisWorkbook.Save
g_db.Close
Set g_db = Nothing
End Sub
I am a new to Excel, VB and using Access for excel through VBA.
I am trying to run a query in a VB macro and return the result to the excel sheet.
I dont get any compiler error but I dont get any result too..
please help..
Here is the code
Public Const DEFAULT_PATH As String = "C:\AccessDatabase"
Public Const DEFAULT_DB As String = "db1.mdb"
Public Sub Aut
On Error Resume Next
Dim rs As Recordset
Dim qy As QueryDef
Dim Row As Integer
Dim Row As Integer
db_name = DEFAULT_PATH & "\" & DEFAULT_DB
If (Len(db_name) > 0) Then
Set g_db = OpenDatabase(db_name)
Else
MsgBox "No DB open!"
End
End If
Set qy = g_db.CreateQueryDef("", "Select * from Table1")
Set rs = qy.OpenRecordset
Row = 1
With rs
Do While Not .EOF
Worksheets("Sheet1").Cells(Row, 1) = rs.Fields(0).Name
Worksheets("Sheet1").Cells(Row, 1) = rs.Fields(1).Name
Row = Row + 1
.MoveNext
End If
Loop
End With
rs.Close
ThisWorkbook.Save
g_db.Close
Set g_db = Nothing
End Sub