Hello Guys ans Gals,
I'm new to writing VBA code so please excuse my ignorance. I have been using the same bit of code to extract data from an Access database (three seperate databases). This time, for some reason, it is not working. See below...
Option Explicit
Const QA As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=\\cherubim\Production Database\Production Database.accdb; Persist Security Info=False;"
Const MiniMRP5 As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=\\cherubim\MRP\MiniMRP5\data\MRP5Data.accdb; Persist Security Info=False;"
Const MiniMRP4 As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=\\cherubim\RW Shares\MiniMRP DataMine\MiniMRP Data Mine.accdb; Persist Security Info=False;"
Private Sub Workbook_Open()
Dim Conn As Object
Dim Data As Object
Dim Field As Object
Set Conn = CreateObject("ADODB.Connection")
Set Data = CreateObject("ADODB.Recordset")
Conn.ConnectionString = MiniMRP5
Conn.Open
On Error GoTo CloseConnection
With Data
.ActiveConnection = Conn
.Source = getSQLString
.LockType = 1
.CursorType = 0
.Open
End With
On Error GoTo CloseRecordset
Worksheets.Add
For Each Field In Data.Fields
ActiveCell.Value = Field.Name
ActiveCell.Offset(0, 1).Select
Next Field
Range("A1").Select
Range("A2").CopyFromRecordset Data
Range("A1").CurrentRegion.EntireColumn.AutoFit
On Error GoTo 0
CloseRecordset:
Data.Close
CloseConnection:
Conn.Close
End Sub
Function getSQLString() As String
Dim JOB As String
Dim SQLstring As String
JOB = Application.InputBox("Enter JOB #")
SQLstring = "SELECT tblworders.SWONo, tblworders.WOQty, tblcustorderdetail.COID, tblstockitems.MasterPNo, tblstockitems.Rev, tblstockitems.ItemDescription, tblstockitems.Cust1, tblstockitems.[OP 10], tblstockitems.[OP 20], tblstockitems.[OP 30], tblstockitems.[OP 40], tblstockitems.[OP 50], tblstockitems.[OP 60], tblstockitems.[OP 70], tblstockitems.[OP 80], tblstockitems.[OP 90], tblstockitems.[OP 100], tblstockitems.Cust3 " & _
"FROM tblworders INNER JOIN (tblcustorderdetail INNER JOIN tblstockitems ON tblcustorderdetail.StockID = tblstockitems.ItemID) ON tblworders.CustORID = tblcustorderdetail.COID " & _
"WHERE (tblworders.SWONo) = ""[JOB]"""
getSQLString = SQLstring
End Function
In this instance, I extract the field headers but none of the actual data stored in the tables is being pulled into Excel. I have a feeling there is some error in my SQL. Can anyone help?
Bookmarks