Darren Smith
Well-known Member
- Joined
- Nov 23, 2020
- Messages
- 631
- Office Version
- 2019
- Platform
- Windows
This code I wrote is to update access queries. But it does not create a record set?
Please help to get the record set working I also think the strQuery is not right?
Please help to get the record set working I also think the strQuery is not right?
VBA Code:
Sub UpdateJobCardMasterLink()
Dim con As Object
Dim rs As Object
Dim AccessFile As String
Dim strQuery As String
Dim i As Integer
Dim ws As Worksheet
Dim LRow As Long
Application.ScreenUpdating = False
AccessFile = "\\TGS-SRV01\Share\ShopFloor\PRODUCTION\DLS Cardworker\Access Files\Job Cards Inventory1.accdb"
strQuery = "Job Card Master Linked Append"
On Error Resume Next
Set con = CreateObject("ADODB.connection")
If Err.Number <> 0 Then
MsgBox "Connection was not created!", vbCritical, "Connection Error"
Exit Sub
End If
On Error GoTo 0
con.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & AccessFile
On Error Resume Next
Set rs = CreateObject("ADODB.Recordset")
If Err.Number <> 0 Then
Set rs = Nothing
Set con = Nothing
MsgBox "Recordset was not created!", vbCritical, "Recordset Error"
Exit Sub
End If
On Error GoTo 0
rs.CursorLocation = 3
rs.CursorType = 1
rs.Open strQuery, con
If rs.EOF And rs.BOF Then
rs.Close
con.Close
Set rs = Nothing
Set con = Nothing
Application.ScreenUpdating = True
MsgBox "There are no records in the recordset!", vbCritical, "No Records"
Exit Sub
End If
Set ws = ThisWorkbook.Worksheets("StockItems")
LRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
ws.Range("A2:E" & LRow).CopyFromRecordset rs
rs.Close
con.Close
Set rs = Nothing
Set con = Nothing
ws.Columns("A:E").AutoFit
Application.ScreenUpdating = True
MsgBox "All data were successfully retrieved from the '" & strQuery & "' query!", vbInformation, "Done"
End Sub