MS Access VBA Dlookup on Yes/No field - vba

I can't for the life of me work out what is wrong with this, but I'm not an Access/VBA developer normally..
I have a database table with about 20 fields, one of which is a Yes/No field. I want to extract the Yes/No value using DLookup, however am receiving the following error:
Run-time error '3075':
Syntax error (missing operator) in query expression 'Enabled'.
The code I am using it:
MsgBox (DLookup("Enabled", "Numbers", "ID = " & Me.cbxNumber.Value & ""))
Enabled is a Yes/No field
ID is a String field.
The above DLookup works absolutely fine for returning String values for other fields, so the last parameter with the search query, and the table field, should be fine. It is simply complaining about the return field ('Enabled') thinking it is a query.
The MsgBox element is also irrelevant, as I have tried assigning the result to an Integer and to a Boolean, and it's not complaining of a type mismatch which I would expect if this were the problematic part.
Any thoughts?

You stated that ID is a string field. If that is the case, try changing the DLookup to...
DLookup("[Enabled]", "Numbers", "ID = " & Chr(34) & Me.cbxNumber.Value & Chr(34))
If ID is a Long, then use this string...
DLookup("[Enabled]", "Numbers", "ID = " & Me.cbxNumber.Value)

Your code works fine for me:
Table:
Form:
Code:
Private Sub Command30_Click()
MsgBox (DLookup("Enabled", "Numbers", "ID = " & Me.cbxNumber.Value & ""))
End Sub
The messagebox displays 0 or -1 as required. Things to check:
Is your code in the forms module? Otherwise Me.cbxNumber.Value won't return anything.
What do you get if you run
debug.print Me.cbxNumber
from the OnClick of a button on the form?

Related

Error 2465 in MS Access when running a VBA code

Read through a few of the previously asked posts about this error but still cannot understand it.
The error text is "Run-time error '2465' CASREP reporting tool can't find the field '|1' referred to in your expression
I have a form to assign parts to certain supply reports. We have a form where users can manually hyperlink the parts to the request if they fall in the cracks. There is a check box feature, followed by the use of an "Assign" button to achieve this.
Every time a box is checked and a user selects "Assign" the error pops up. The string of code that pops up with the bug is as follows:
strSQL_del = "DELETE FROM [tbl_Temp_Assign] " & _
"WHERE [jcn_cd]= '" & Forms![Parts: Assign Unit].[tblTemp_Assign subform1].Form.[jcn.cd] & "'
AND [CASREP #] = '" & strCASREP & "' AND [doc_num_cd] = '" & Forms![Parts: Assign Unit].[tblTemp_Assign subform1].Form.[doc_num_cd] & "'
AND [last_updated_dte] = #" & Forms![Parts: Assign Unit].[tblTemp_Assign subform1].Form.[last_updated_dte] & "#
AND [PMOMsgDte] = #" & Forms![Parts: Assign Unit].[tblTemp_Assign Unit].Form.[PMOMsgDte] & "#" 'Intentionally left off AssignDte
Any help is greatly appreciated. I'm lost on what is wrong.
The error is related to the SQL string concatenation, but that string is quite long, it's hard to say if you have a space between ' and the first AND condition. Also, after the first continuation character _, is all that a single line?
Anyway, I would strongly advise to setup a delete query where you pass the form values as parameters and in VBA you just execute the query. It will be easier to update later on if needed.
The query:
PARAMETERS [prmTextField] Text (50), [prmDateField] DateTime, [prmNumberField] Long;
DELETE *
FROM T
WHERE T.TextField = [prmTextField]
AND T.DateField = [prmDateField]
AND T.NumberField = [prmNumberField];
Then, in VBA just execute the query.
With CurrentDb().QueryDefs("QueryName")
.Parameters("[prmTextField]").Value = '[Value from Form (text)]
.Parameters("[prmDateField]").Value = '[Value from Form (date)]
.Parameters("[prmNumberField]").Value = '[Value from Form (number)]
.Execute dbFailOnError
End With
By the way, do you really need to pass all those parameters to delete a record? Usually, just the record id is needed.

DLookup When Trying Use Form Field as Criteria

I am new to Access and also to VB. I have a report that displays information on transformers, the report displays data from a query that queries data from two different tables. I would like to use a button to open a PDF document saved in one of the tables.
The code I have so far is this:
Private Sub Command70_Click()
Dim hypa As String
hypa = DLookup("[TestReport]", "TransformerPics", "TxID = " & [Reports]![TransformerInfoSheet]!TXID)
Application.FollowHyperlink [hypa]
End Sub
The error i get is Run-time error '2471' The expression you have entered as a parameter produced this error: 'TP00686'
TP00686 is the transformer number that is displayed on the report.
You need to have some quotes in there:
hypa = DLookup("[TestReport]", "TransformerPics", "TxID = '" & [Reports]![TransformerInfoSheet]!TXID & "'")
Please also see:
DLookup Usage Samples
Dlookup using a string variable as criteria parameter

How do I prevent duplicate entries in my Access database?

I am a first time coder with VBA and I am creating a database for data entry at a Psych Lab I work at. Currently the database is created, but I want to prevent duplicate entries from being put into the database (namely by have a code look for the participant number right after it is entered). I have been trying to fix this code for quite a while and I just recently hit a wall. It displays the correct error message when I enter the participant number, however it says that every number has been entered already (even though they actually haven't). Here is the code:
Private Sub Participant_Number_BeforeUpdate(Cancel As Integer)
Dim Participant_Number As Integer
Dim StLinkCriteria As Integer
If (Not IsNull(DLookup("[Participant_Number]", "Entry Log", "[Participant_Number] ='" & Me.Participant_Number.Value & "'"))) Then
MsgBox "Participant Number has already been entered in the database."
Cancel = True
Me.Participant_Number.Undo
End If
End Sub
Any help is greatly appreciated. I have never used VBA before and I am self-teaching how to code.
I guess your Participant_Number field is a number. You shouldn't enclose the criteria with single-quotes ', these are used with fields of text type. Try changing the criteria field from
"[Participant_Number] ='" & Me.Participant_Number.Value & "'"))) Then
into
"[Participant_Number] = " & Me.Participant_Number.Value))) Then
IF you have not used VBA you may try to do this by opening the table in Design view. This method is easy and a good choice. You may have a look here: https://support.office.com/en-us/article/Prevent-duplicate-values-in-a-field-b5eaace7-6161-4edc-bb90-39d1a1bc5576?ui=en-US&rs=en-US&ad=US&fromAR=1

Error #Name? in access form with DLookup

I have the error #Name? in a form in Access 2013 in a textbox that have as origin a DlookUp function.
Specifically I need to do the same thing that is on this topic: automatically update the field Costo Unitario field based on the selection of the field Operatore
The combobox of the Operatore field takes the values from a table named Operatori that it is like that:
Operatore CostoOp_Unitario
--------- --------
Marco 20,07
Giuseppe 20,02
Gennaro 19,69
Pierangelo 17,05
The function that is set up as Control Source for the textbox that gives the #Name? error is:
=DLookUp([CostoOp_Unitario];[Operatori];"[Operatore]=""" & [OperatoreComboBox].[Text] & """")
based on the function that is present in the thread that I linked above. OperatoreComboBox is the combobox Operatore in the image. Why it outputs this error?
I think your syntax is not correct. Try this one:
=DLookUp("[CostoOp_Unitario]","Operatori","[Operatore] = '" & [OperatoreComboBox] & "'")

How to stop Access from thinking my table.field in VBA is a form component?

So I'm writing a SQL query in VBA in Access 2010, and when this code is ran, it thinks that SupplierConnect.MailboxID is a component on a form, where it is in fact a database table (SupplierConnect) and field (MailboxID). Every time that code is ran it pops up a box asking me for input from that form component, which actually isn't one. Is there any way to get around this or code this differently?
Thanks!
' Mailbox ID
If IsNull(MailboxIDComboBox.Value) Then
Else
If firstWhere = True Then
MailboxID = "WHERE SupplierConnect.MailboxID = '" & [Forms]![SupplierQuery]!MailboxIDComboBox.Value & "'"
firstWhere = False
Else
MailboxID = " AND SupplierConnect.MailboxID = '" & [Forms]![SupplierQuery]!MailboxIDComboBox.Value & "'"
End If
End If
This is not popping up asking you for this value because it thinks it is a form control, it is because it is undefined to the query. It cannot find the field within query definition. This usually happens because you do not have the proper case sensitive table or field id or the incorrect table is linked. If you provided more code for the SQL statement you are using from beginning to end and your Exact table structure we can narrow down the mistake.