Access DlookUp not working with multiple Criteria - vba

The Dlookup Function is Giving me a syntax error I have tried multiple ways but none worked
x = DLookup("Clock_ID", "User_access", "Clock = " '" & Me.Clock_ID & _
& " AND Module = '" & Me.m_1 & "'")
I basically Need it to check 2 fields in the same table at the same time so the ID can be duplicated But the Module cant be duplicated.

I guess ID is numeric, so try:
x = DLookup("Clock_ID", "User_access", "Clock = " & Me.Clock_ID & " AND Module = '" & Me.m_1 & "'")

Related

Ms access Run time error 2471 only if there is letter in the text box

I'm using this
If DCount("[ITEMCODE]", "LOCALTEMPORARYIMPORTS", "[itemcode]= '" & Me![ITEMCODE2] & "' AND [INVOICENUMBER] like " & Me.INVOICENUMBER) > 0 Then
to check in "localtemporaryimports" table if "itemcode" and "invoicenumber" already exist.
if "invoicenumber" has letter i get the error. And if i remove the letter everything is working fine.
What i'm i missing?
using office 365
As you look for an exact invoice number that is text, filter as you do for the itemcode:
If DCount("[ITEMCODE]", "LOCALTEMPORARYIMPORTS", "[itemcode]= '" & Me![ITEMCODE2] & "' AND [INVOICENUMBER] = '" & Me!INVOICENUMBER & "'") > 0 Then
However, as you don't need to count a unique value, use DLookup:
If Not IsNull(DLookup("[ITEMCODE]", "LOCALTEMPORARYIMPORTS", "[itemcode]= '" & Me![ITEMCODE2] & "' AND [INVOICENUMBER] = '" & Me!INVOICENUMBER & "'")) Then

Filter Access form by multiple filters

I have a form that filters a database using multiple filters. However all filters need to be entered in or have a value for the table to be filtered correctly.
I would like to be that not all inputs are required to filter the table.
I'm not sure if thats worded correctly.
Please see what I have tried so far.
DoCmd.ApplyFilter _
"select * from SageOrderLine_Live where " & _
"[PromisedDeliveryDate] = " & Format(Me.DateFrom, "\#mm\/dd\/yyyy\#") & "
and " & _
"[CustomerAccountNumber] = """ & Me.CustomerAccountNumber & """" & " and
" & _
"[Code] = """ & Me.Codes & """" & " and " & _
"[AnalysisCode1] = """ & Me.Analysis & """" & " Or " & _
"[AnalysisCode2] = """ & Me.Analysis & """" & " Or " & _
"[AnalysisCode3] = """ & Me.Analysis & """"
Consider using a parameterized query avoiding concatenation and quote punctuation by directly referencing form controls. Use such a query in form RecordSource calling NZ to assign missing search parameters to column itself. Logically this will render all search parameters optional. Do note below does not considsr NULL values.
SQL (save as stored query object to be used as form's RecordSource)
SELECT * FROM SageOrderLine_Live
WHERE [PromisedDeliveryDate] = NZ(Forms!myform!DateFrom, [PromisedDeliveryDate])
AND [CustomerAccountNumber] = NZ(Forms!myform!CustomerAccountNumber, [CustomerAccountNumber])
AND [Code] = NZ(Forms!myform!Codes, [Code])
AND [AnalysisCode1] = NZ(Forms!myform!Analysis, [AnalysisCode1])
AND [AnalysisCode2] = NZ(Forms!myform!Analysis, [AnalysisCode2])
AND [AnalysisCode3] = NZ(Forms!myform!Analysis, [AnalysisCode3])
VBA
Me.Form.Requery

Issue with SQL code in Microsoft Access

I have a button in my database that is supposed to find a record when it is clicked. The issue I am encountering is that I want it to search for a record based on two fields.
This is my code:
I am using the SearchForRecord macro with Where Condition
="[Short Title] = " & "'" & [Combo101] & "'" And "[Baseline] = " & "'" & [Combo103] & "'"
It is not liking this. If I just have the Where condition as
="[Short Title] = " & "'" & [Combo101] & "'"
or I have it as
="[Baseline] = " & "'" & [Combo103] & "'"
then it works fine. But when I try to combine the two (which I need to do) it will not find any records.
I tried to break it up into two separate SearchForRecord macros and while that did return records when I clicked the button, it still wasn't working properly.
It would be ideal if someone could let me know why my original code was not working and what needs to be done to fix it.
Try:
="[Short Title] = '" & [Combo101] & "' AND [Baseline] = '" & [Combo103] & "'"

SQL syntax error on vba

I made a SQL statement in the add/update button in the query wizard I changed it back to SQL view to see how the program made me the code and when I copy and paste the same error on the If statement of the btnAdd it throws me a syntax error, but how?
here is the entire code:
Private Sub cmdAdd_Click()
'In the button add we have two options
'1. Insert
'2. Update
If Me.txtID.Tag & "" = "" Then
CurrentDb.Execute "INSERT INTO tblClients ( ClientID, ClientName, Gender, " & _
"City, [Address (Fisical)], [Cellphone/Telephone] ) " & _
"SELECT " & Me.txtID & ",'" & Me.txtName & "','" & Me.cboGender & "', '" & Me.cboCity & "','" & Me.txtAddress & "','" & Me.txtCellphone & "'"
Else
'Otherwise the data will be updated
CurrentDb.Execute "UPDATE tblClients SET tblClients.ClientName = [me]. [txtName], tblClients.Gender = [me].[cboGender], tblClients.City = [me].[cboCity], tblClients.[Address (Fisical)] = [me].[txtAddress], tblClients.[Cellphone/Telephone] = [me].[txtCellphone] "
WHERE (([ClientID]=[Me].[txtID].[Tag]));
End If
cmdClear_Click
tblClients_subform.Form.Requery
End Sub
it highlights me this row in red:
WHERE (([ClientID]=[Me].[txtID].[Tag]));
It appears that the following code is not on the same line
CurrentDb.Execute "UPDATE tblClients SET tblClients.ClientName = [me]. [txtName], tblClients.Gender = [me].[cboGender], tblClients.City = [me].[cboCity], tblClients.[Address (Fisical)] = [me].[txtAddress], tblClients.[Cellphone/Telephone] = [me].[txtCellphone] "
WHERE (([ClientID]=[Me].[txtID].[Tag]))
So you may want to change it to
CurrentDb.Execute "UPDATE tblClients SET tblClients.ClientName = [me]. [txtName], tblClients.Gender = [me].[cboGender], tblClients.City = [me].[cboCity], tblClients.[Address (Fisical)] = [me].[txtAddress], tblClients.[Cellphone/Telephone] = [me].[txtCellphone] " & _
"WHERE (([ClientID]=[Me].[txtID].[Tag]))"
In addition to Cableload's correct answer where the WHERE statement that was on a new code line was not connected to the previous line by the use of an underscore at the end of the first one, there is still a referncing issue.
You are referencing values in a UserForm like that were columns in a table so it is not finding the value you are looking for. To get the value into the SQL statement you need to come out of the literal string, reference the value, and then continue writing the string (not forgetting to enclose the value with '): -
CurrentDb.Execute "UPDATE tblClients SET " & _
"[ClientName] = '" & Me.txtName & "', " & _
"[Gender] = '" & Me.cboGender & "', " & _
"[City] = '" & Me.cboCity & "', " & _
"[Address (Fisical)] = '" & Me.txtAddress & "', " & _
"[Cellphone/Telephone] = '" & Me.txtCellphone & "' " & _
"WHERE [ClientID]=" & Me.txtID.Tag
I have spread it across multiple lines for ease of reading but obviously you can adjust your actual code however needed.
I would also question [ClientID]=" & Me.txtID.Tag, is the ClientID in the in the txtID.value or the txtID.Tag, they are different places. The value property is the value in the text box, the Tag property is more like a area for metadata that you can populate if needed but is not automatically populated by default.
Finally I'd like to refer you back to an answer to a previous question you had, at the bottom of the answer there was a tip about placing the resultant query into a Access Query in SQL view to get better information on the error, that would have helped you here too. To give further assistance on the 'resultant query'.
In debug mode before the while the CurrentDb.Execute is highlighted but before it is run (using F8 to step through each line until you get there, or placing a breakpoint on that line
Open the the Immediate Window if it is not already open (either Ctrl+G to from the menu bar 'View' > 'Immediate Window')
Copy all related code from the line after the CurrentDb.Execute statement, in this case it would be UPDATE ... .Tag
In the immediate window type a question mark and then paste in the rleated code and press enter
The immediate window will return the resultant string for you to try in a Query in SQL view.
Change the SELECT keyword to VALUES in your INSERT statement.
CurrentDb.Execute "INSERT INTO tblClients ( ClientID, ClientName, Gender, " & _
"City, [Address (Fisical)], [Cellphone/Telephone] ) " & _
"VALUES (" & Me.txtID & ",'" & Me.txtName & "','" & Me.cboGender & "', '" & Me.cboCity & "','" & Me.txtAddress & "','" & Me.txtCellphone & "')"
And the UPDATE should be this. The issue here was that you were trying to use Form controls in the SQL, but you needed to evaluate the controls first then concatenate their values to your literal string.
I'm wondering if you really need Me.txtID instead of Me.txtID.Tag
So sway that out if it doesn't work.
CurrentDb.Execute "UPDATE tblClients SET tblClients.ClientName = '" & me.txtName & "', tblClients.Gender = '" & me.cboGender & "', tblClients.City = '" & me.cboCity & "', tblClients.[Address (Fisical)] = '" & me.txtAddress & "', tblClients.[Cellphone/Telephone] = '" & me.txtCellphone & "' WHERE (([ClientID]=" & Me.txtID.Tag & "));"

Trouble using variables in VBA SQL WHERE Clause

I am trying to update a table using variables in VBA for Access. The statement is below.
DB.Execute "UPDATE tblSearchersList SET '" & vSearcherDay & "' = " & VHours & "
WHERE Member= '" & Me.cboMember.Column(1) & "'AND [Mission] = '" & Me.Mission & "'"
tblSearcherList is table to update
vSearcherDay is a variable that combines the letter "d" with a number, et(1,2,3,4,5) depending on other query
VHours is a decimal number (number of hours)
Member is a text value from Form Field Me.cboMember.Column(1)
Mission is a text value from form field Me.Mission
I get Runtime error 3061 - Too few parameters expected 2.
Hope I can get some help with this as I have been fighting it for awhile and am losing the battle.
Thanks
New code is this:
Sorry bout the comments thing. I am new and didn't quite know how to do this.
DB.Execute "UPDATE tblSearchersList SET " & vSearcherDay &_
" = " & VHours & " WHERE Member= '" & Me.cboMember.Column(1) & "' &_
" And [Mission] = '" & Me.Mission & "'"
I am quite embarrassed about this but I had the Member field name wrong. Should've been
MemberName instead. I really do appreciate all the quick help I got and will do better next time. It works perfectly. Thank you all.
Don't use apostrophes around field name. Instead
SET '" & vSearcherDay & "' = " &
do
SET " & vSearcherDay & " = " &