Revised question: I'm not used to writing SQL queries in VBA so I've been using the macro recording for connecting to Access. I've included the code that the macro recorder returns. I'm getting an error message
Unexpected Error. Something went wrong. If the problem continues, please restart Excel.
If I click close on the error message then the spreadsheet with the database info from Access shows up in Excel, which is great, but I'd rather not have the error message pop up.
Here's my code so far:
Sub Contact_Search()
Dim ContactNum As String
Restart:
ContactNum = InputBox("Enter the number to query.", "Contact Query", "Enter the number here...")
If ContactNum = "Enter the number here..." Then
MsgBox "Invalid response, please enter the number to query."
GoTo Restart
ElseIf ContactNum = "" Then
MsgBox "Number is mandatory. Please enter number."
GoTo Restart
End If
ActiveWorkbook.Worksheets.Add After:=Sheets(1)
With ActiveSheet.ListObjects.Add(SourceType:=0, Source:=Array( _
"OLEDB;Provider=Microsoft.ACE.OLEDB.12.0;Password="""";User ID=Admin;Data Source=C:\Users\path info\folder name\Contacts " _
, _
"Database.accbd;Mode=Share Deny Write;Extended Properties="""";Jet OLEDB:System database="""";Jet OLEDB:Registry Path="""";Jet OLEDB:Da" _
, _
"tabase Password="""";Jet OLEDB:Engine Type=6;Jet OLEDB:Database Locking Mod=0;Jet OLEDB:Global Partial Bulk Ops=2;Jet OLEDB:Globa" _
, _
"l Bulk Transactions=1;JetOLEDB:New Database Password="""";Jet OLEDB:Create System Database=False;Jet OLEDB:Encrypt Database=False" _
, _
";Jet OLEDB:Don't Copy Local on Compact=False;Jet OLEDB:Compact Without Replica Repair=False;Jet OLEDB:SFP=False;Jet OLEDB:Suppo" _
, _
"rt Complex Data+False;Jet OLEDB:Bypass User Info Validaton=False;Jet OLEDB:Limited DB Caching=False;Jet OLEDB;Bypass ChoiceField" _
, " Validation=False"), Destination:=Range("$A$1")).QueryTable
.CommandType = xlCmdTable
.CommandText = Array("Contacts")
.PreserveFormatting = True
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.PreserveColumnInfo = True
.SourceDataFile = "C:\Users\path info\folder name\Contacts\Database.accdb"
.ListObject.DisplayName = "Table_Database.accdb"
.Refresh BackgroundQuery:=False
End With
End Sub
I'm sure that much of what's in this code is not really needed, it's just stuff that the macro recorder puts in there but I'm not sure what is ok to take out and what has to be in there for it to work and I'm not sure if something in the code is causing the error message that I'm getting. As I said, the info is still coming over, but I have to close out of the error message before it shows up in the excel doc.
Also, what I'm actually wanting to return is not the entire table, but only the rows that match the variable ContactNum that is input by the user. I'm not sure where in this code I would put the SQL language to only return the specific values rather than the entire table. Any ideas on the error message as well as the SQL verbiage?