I was wondering why am I getting this error? - vba

(fake entries and file directories)
This is the error I got when trying to run a macro that takes values from a query and assigns them to a bookmark'd location on a word .docx
this is the line it fails on
Set rs = CurrentDb.OpenRecordset("Query1", dbOpenDynaset)
this is the query that the vba code is taking values from
SELECT Table1.ProjectNo, Table1.FirstName, Table1.LastName, Table1.Phone, Table1.ProjectName, Table1.EMail, Table1.Client
FROM Table1
WHERE (((Table1.LastName) Like "*" & [Forms]![Form3]![SearchBox] & "*"));
Private Sub Command9_Click()
Dim wApp As Word.Application
Dim wDoc As Word.Document
Dim rs As DAO.Recordset
Set wApp = New Word.Application
Set wDoc = wApp.Documents.Open("C:\Documents\ECORtester.docx")
Set rs = CurrentDb.OpenRecordset("Query1", dbOpenDynaset)
If Not rs.EOF Then rs.MoveFirst
Do Until rs.EOF
wDoc.Bookmarks("Attention").Range.Text = Nz(rs![FirstName], "") & Nz(rs![LastName], "")
wDoc.Bookmarks("Client").Range.Text = Nz(rs![Client], "")
wDoc.Bookmarks("Email").Range.Text = Nz(rs![Email], "")
wDoc.Bookmarks("Phone").Range.Text = Nz(rs![Phone], "")
wDoc.Bookmarks("ProjectName").Range.Text = Nz(rs![ProjectName], "")
wDoc.Bookmarks("ProjectNumber").Range.Text = Nz(rs![ProjectNo], "")
wDoc.SaveAs2 "C:\Documents" & rs!ProjectNumber & "_ECORtester.docx"
rs.MoveNext
Loop
End Sub

Your WHERE clause has a parameter in it that isn't being set.
Define it as a querydef and create the recordset from that.
Dim qdf As QueryDef
Dim rs As Recordset
Set qdf = CurrentDb.QueryDefs("Query1")
qdf.Parameters("[Forms]![Form3]![SearchBox]") = [Forms]![Form3]![SearchBox]
Set rs = qdf.OpenRecordset

Recordset based on query object that has undefined/unset dynamic parameter won't work. Instead, base recordset on table and build SQL in VBA.
strSQL = "SELECT * FROM Table1 WHERE LastName Like "*" & [Forms]![Form3]![SearchBox] & "*"
Set rs = CurrentDb.OpenRecordset(strSQL, dbOpenDynaset)
Using LIKE with wildcard could return multiple clients. If there are multiple people with same/similar last name, they could all be retrieved. If you want only one client, then use unique record ID as filter criteria. A combobox may be more useful than a free-form input textbox. With code, combobox can implement pattern matching and 'filter as you type' functionality but then selection is made from listed items and unique ID is available.

Related

Access VBA change Query criteria via VBA

I am trying to change the SQL statement of a query.
This is the following code:
Dim db As DAO.Database
Dim qdf As DAO.QueryDef
Dim sqlString As String
Set db = CurrentDb()
Set qdf = db.QueryDefs("Query1")
sqlString = "SELECT *all the Tables* FROM tab WHERE (((tab.columname)=*Variable*;"
qdf.SQL = sqlString
so my problem is the value of the Variable is not shown...
but it has to ge out of the string so I tried this:
sqlString = "SELECT *all the Tables* FROM tab WHERE (((tab.columname)= " & " *Variable*" & " ;"
I had to add "Variable" (quotation marks)
this then works half way.
it then uses the value of the variable it is set to but the quotes are missing...
The Goal of this Code is to change the Criteria that shows the data in the query.
The Variable value is as sample LA-85995561.
The way i want to change the value is via SQL statement.
I don't know further from here. thanks for any help!
(I am new to VBA)
Classic example to use parameterization, an industry best practice when using SQL at the application layer like VBA. And since you already use a querydef you can pass parameters easily withe PARAMETERS clause to define data type and placeholder.
SQL (save as a stored query)
PARAMETERS VariableParam Text(255);
SELECT * FROM tab WHERE tab.collumname = [VariableParam];
VBA
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim qdf As DAO.QueryDef
Dim sqlString As String
Set db = CurrentDb()
Set qdf = db.QueryDefs("mySavedQuery")
qdf!VariableParam = "LA-85995561"
Set rs = qdf.OpenRecordset()
' rs CAN BE USED IN FORMS/REPORTS RECORDSOURCES:
' Set Me.Form.Recordset = rst
' Set Me.Report.Recordset = rst
You could use PARAMETERS:
This passes a text string:
Sub Test()
Dim db As DAO.Database
Dim qdf As DAO.QueryDef
Dim rst As DAO.Recordset
Set db = CurrentDb
Set qdf = db.CreateQueryDef("", "PARAMETERS MyNamedSearchValue TEXT; " & _
"SELECT * FROM MyTable WHERE columname = MyNamedSearchValue")
With qdf
.Parameters("MyNamedSearchValue") = "A"
Set rst = .OpenRecordset
End With
With rst
MsgBox .Fields("MyDateField")
.Close
End With
qdf.Close
End Sub
This passes a number:
Sub Test()
Dim db As DAO.Database
Dim qdf As DAO.QueryDef
Dim rst As DAO.Recordset
Set db = CurrentDb
Set qdf = db.CreateQueryDef("", "PARAMETERS MyNamedSearchValue LONG; " & _
"SELECT * FROM MyTable WHERE columname = MyNamedSearchValue")
With qdf
.Parameters("MyNamedSearchValue") = 100
Set rst = .OpenRecordset
End With
With rst
MsgBox .Fields("MyDateField")
.Close
End With
qdf.Close
End Sub
You'll have to update the SQL to suit your needs. You could also pass it the name of a stored query rather than writing the SQL within the procedure.
If columname is text, then you can use something like this:
sqlString = "SELECT * FROM tab WHERE tab.columname='" & strVariable & "'"
For numeric data type:
sqlString = "SELECT * FROM tab WHERE tab.columname=" & str(lngVariable)
The Anser is that I had to use different marks:
WHERE (((tab.collumname)=""" & strVariable& """));"
it's about the """ & part it never made the " in the SQL thanks for all awnsers! brought me to my solution! <3

Erase Data from a Field without a name acess vba

I am trying to erase some data (values = 0) from a field in access, but without knowing the name, I only know the position of the field in the database (13Âșcolumn) . I thought it would be very simple, but after many tries I still can't manage to find a solution and I am starting to doubt if it's even possible.
So this is the code that I am using:
Sub Erasevalues0()
Dim strQValue As String
Dim i As Integer
Dim db As DAO.Database
Dim sql13 As String
Set db = CurrentDb
sql13 = db.TableDefs("TableName").Fields(13).Name
strQValue = "DELETE FROM TableName WHERE sql13=0;"
db.Execute strQValue
End Sub
I even tried something like:
strQValue = "DELETE FROM TableName WHERE Fields(13)=0;"
But nothing seems to work. Any suggestions?
I think you need to loop through the recordset. The following should work
Sub RemoveZeros()
Dim db As Database
Dim rs As Recordset
Set db = CurrentDb
Set rs = db.OpenRecordset("Table1", dbOpenDynaset)
rs.MoveFirst
With rs
Do Until .EOF
If .Fields(13) = 0 Then .Delete 'Delete row if column 13 = 0
.MoveNext 'Move to next record
Loop
End With
rs.Close
Set rs = Nothing
Set db = Nothing
End Sub
Not sure if there is a direct way to use the index of a field. A workaround:
query = "SELECT * FROM TableName"
columnName = CurrentDb.OpenRecordset(query).Fields(13).Name
strQValue = "UPDATE TableName " & _
"SET " & columnName & "=''"

Comparing recordset values in Access VBA

I am trying to compare two recordsets in access VBA to check whether the values within the two tables are the same or whether they differ. Both recordsets have the same structure (field headings) and record IDs and I'm trying to check whether a field value for a record matches the corresponding field value in the second recordset. The record ID field name is MATNR.
I think I've managed to loop through the records and fields for the 1st recordset but I'm unsure how to loop through and compare these records with the second. Also, is there a smarter way to compare the recordsets other than If rs1.Fields(fld.Name) = rs2.Fields(fld.Name)
Any help will be greatly appreciated.
Public Sub VerifyRecords()
Dim rs As DAO.Recordset
Dim rs1 As DAO.Recordset
Dim rs2 As DAO.Recordset
Dim rs3 As DAO.Recordset
Dim fld As DAO.Field
Dim sSQL As String
Dim sSQL1 As String
Dim sSQL2 As String
Set rs = CurrentDb.OpenRecordset("R2_Tables_to_Compare1") 'This table lists the upload tables to query and their corresponding target tables
Set rs3 = CurrentDb.OpenRecordset("RecordValueComparisonResults") 'Write the results of the record vlaue comparison to here
'**************************************************************************************
'This SQL statement selects all records from the upload table
sSQL = "SELECT * "
sSQL = sSQL & " FROM " & rs(0)
Set rs1 = CurrentDb.OpenRecordset(sSQL)
'**************************************************************************************
'This SQL statement selects only those records that are applicable in the target table
sSQL1 = "SELECT " & rs(1) & ".* FROM " & rs(1) & " INNER JOIN " & rs(0) & " ON " & rs(1) & ".MATNR = " & rs(0) & ".MATNR"
Set rs2 = CurrentDb.OpenRecordset(sSQL1)
'**************************************************************************************
Do While Not rs1.EOF
For Each fld In rs1.Fields
If rs1.Fields(fld.Name) = rs2.Fields(fld.Name) Then
Debug.Print rs1.Fields("MATNR"), rs2.Fields("MATNR"), fld.Name, rs1.Fields(fld.Name), rs2.Fields(fld.Name)
End If
Next fld
rs1.MoveNext
Loop
rs.Close
rs1.Close
rs2.Close
rs3.Close
Set rs = Nothing
Set rs1 = Nothing
Set rs2 = Nothing
Set rs3 = Nothing
End Sub
Below are two options, although the QUERY OPTION is faster and better practice when working in Access and any relational DB:
QUERY OPTION: This query could be passed into a recordset and the recordset would contain only the matching values between the fields in the two tables. Then you could loop through that new recordset and print or process as necessary using a single loop.
SELECT column_name FROM table1 INNER JOIN table2 ON table1.column_name = table2.column_name;
LOOP OPTION: If you are intent on looping through both recordsets, use this code. There is probably a more efficient way to do this, especially since this method uses four nested loops which is a no-no. I would highly recommend the QUERY OPTION.
While Not rs1.EOF
While Not rs2.EOF
For Each fld1 in rs1.Fields
For Each fld2 in rs2.Fields
If rs1.Fields(fld1.Name) = rs2.Fields(fld2.Name) Then
Debug.Print rs1.Fields("MATNR"), rs2.Fields("MATNR"), fld1.Name,
rs1.Fields(fld1.Name), rs2.Fields(fld2.Name)
End If
Next fld2
Next fld1
rs2.MoveNext
Wend
rs2.MoveFirst
rs1.MoveNext
Wend

How to use variable initiated in SQL query?

I made a query to get Max value of a certain column
sSQLmax = "SELECT MAX([tablename]!rowname) as MaxNum FROM [tablename]"
I want to use MaxNum value. I tried it in a Msgbox and gave me a blank value. I tried all these:
MsgBox "MaxNum" & MaxNum
MsgBox "MaxNum" & sSQLMAX.MaxNum
MsgBox "MaxNum" & sSQLMAX!MaxNum
More of the code
Dim db As DAO.Database, rst As DAO.Recordset
Set db = CurrentDb
Set rst = db.OpenRecordset(sSQLmax)
but none works, Im sure its simple but it escapes me
With your code you are only setting a variable to a string. You are not actually doing anything with it. This is probably more of what you are looking for:
Dim db As DAO.Database, rst As DAO.Recordset, sSQLmax As String
sSQLmax = "SELECT MAX([tablename].rowname) as MaxNum FROM [tablename]"
Set db = CurrentDb
Set rst = db.OpenRecordset(sSQLmax, dbOpenDynaset)
If (rst.RecordCount <> 0) Then
rst.MoveFirst
MsgBox "MaxNumber: " & rst.Fields("rowname")
End If
Try the DMax function
Dim result as String
result = DMax("[COLUMN NAME]", "[TABLE NAME]")

Export Query in VBA loop to select data based on String Value

I have a table called TEST, which I have code below that loops an export query based on unique values in the column Territory.
The code is supposed to export data to excel files based on the unique values in the column Territory. So each Territory value would have it's own file.
I am having trouble with setting up the sql query and how to use the string value to select the data:
Sub TEST()
Dim db As DAO.Database
Dim rs1 As DAO.Recordset
Dim v As String
Set db = CurrentDb()
Set rs1 = db.OpenRecordset("Select Distinct Territory From TEST")
Do While Not rs1.EOF
v = rs1.Fields(0).Value
**DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel8, _
"WHAT SHOULD MY QUERY BE TO USE STRING v?", "C:\Users\me\Desktop\VBA_TEST\" v & ".xls", True**
rs1.MoveNext
Loop
rs1.Close
End Sub
Can someone guide me into how I may write the query and connect the string v so that it loops out reports?
Thank you!
I think you are required to use an existing query and not just your query as a string for the TransferSpreadsheet method. This means you will need a temporary query object to transfer your spreadsheet.
You can add a variable to query by joining it to the SQL string making sure that for text fields you include an ' on either side and leave it off for numeric fields.
Sub TEST()
Dim db As DAO.Database
Dim rs1 As DAO.Recordset
Dim v As String
Set db = CurrentDb()
Set rs1 = db.OpenRecordset("Select Distinct Territory From TEST")
Dim strQry As String
Dim qdfTemp As DAO.QueryDef
Dim strQDF As String
strQDF = "_TempQuery_"
Do While Not rs1.EOF
v = rs1.Fields(0).Value
strQry = "SELECT * FROM TEST WHERE Territory = '" & v & "'"
Set qdfTemp = CurrentDb.CreateQueryDef(strQDF, strQry)
qdfTemp.Close
Set qdfTemp = Nothing
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel8, _
strQDF, "C:\Users\me\Desktop\VBA_TEST\" & v & ".xls", True
CurrentDb.QueryDefs.Delete strQDF
rs1.MoveNext
Loop
rs1.Close
End Sub