how to auto-fill text box with the dlookup function in access - ms-access

I am trying to auto-full a text box using the Dlookup function, as below:
=DLookUp("[CustomerFACT]![ID name]","CustomerFACT","[CustomerFACT]![ID number] = " & [Forms]![NewLoanForm_2]![ID number])
My original column names have spaces between them
It seems that the code works, only after I input an ID, it fills the text box with #Error
Thanks in advance

Try this simpler syntax:
=DLookUp("[ID name]","[CustomerFACT]","[ID number] = " & [ID number] & "")
or, if ID number is text:
=DLookUp("[ID name]","[CustomerFACT]","[ID number] = '" & [ID number] & "'")

Related

Multiple filter clauses for query and subsequent report from unbound form controls, button activated

Been a while since I used Access and now stuck with a reporting problem.
I have a form, with various unbound controls. There a start date, end date and three levels of business/asset/location in combo boxes.
I can get my query and report to work for each of these as individual 'where' clauses when clicking on a button. That's fine.
However I would like to know what code I need to use when clicking a button so I can combine one or more of the above controls to filter the report i.e. my date range + Business, or date + Business + Asset etc.
I have been trawling the internet and testing different variations but seem to have gone through the error book so far.
My latest effort (on click) as one long string gives me a data mismatch error. If I remove the BU/Asset/Facility parts then my date range code does work. However, it's the combination of these I want to filter by.
DoCmd.OpenReport ReportName:="rptVerification_Results", View:=acViewPreview, WhereCondition:="[Date Entered] Between #" & Me.StartDate & "# AND #" Me.EndDate & "#""" And "BU = " & Me.cboBusiness & "" And "Asset = " & Me.cboAsset & "" And "Facility = " & Me.cboFacility & ""
As you can probably tell I'm winging it and need some direction please.
Thanks
It can be tricky to get the combination of quoted strings and form fields right, as you need to be aware of which quotes are being used to concatenate the WhereCondition string together and which quotes are being presented to the query engine. You also need to know which fields are text and which are numeric, because text fields need to be enclosed in quotes in the resulting string, while numerics don't. I'm assuming below that cboBusiness, cboAsset and cboFacility are all text.
I suggest you create a separate variable to store your WhereCondition in:
Dim myWhereCondition As String
myWhereCondition = "[Date Entered] BETWEEN #" & Me.StartDate & "# AND #" & Me.EndDate
& "# AND BU = '" & Me.cboBusiness
& "' AND Asset = '" & Me.cboAsset
& "' AND Facility = '" & Me.cboFacility & "'"
DoCmd.OpenReport ...
WhereCondition:=myWhereCondition
You can then create a debug breakpoint on the "DoCmd" statement and check the value of "myWhereCondition" in the Immediate window, to make sure it is formed correctly, before DoCmd runs.
IIRC, you can use apostrophes/single quotes as an alternative to double quotes in MS Access, as I've done above. If this is not the case, then each of the single quotes above would need to be converted to "double double quotes" (because a double quote on its own would terminate the string).
The somewhat messier "double quotes everywhere" version of the WhereCondition would be:
myWhereCondition = "[Date Entered] BETWEEN #" & Me.StartDate & "# AND #" & Me.EndDate
& "# AND BU = """ & Me.cboBusiness
& """ AND Asset = """ & Me.cboAsset
& """ AND Facility = """ & Me.cboFacility & """"
Note that if any of the cbo fields are numeric, you need to remove the corresponding single (or double double) quotes from each side of that field.
Try this WhereCondition:
Dim WhereCondition As String
' First, adjust the WhereCondition:
WhereCondition = "([Date Entered] Between #" & Format(Me!StartDate.Value, "yyyy\/mm\/dd") & "# And #" & Format(Me!EndDate.Value, "yyyy\/mm\/dd") & "#) And BU = '" & Me.cboBusiness & "' And Asset = " & Me.cboAsset & " And Facility = " & Me.cboFacility & ""
' Then, open report:
DoCmd.OpenReport ReportName:="rptVerification_Results", View:=acViewPreview, WhereCondition:=WhereCondition

ms access dynamically change label on report

I am printing a production ticket as a report using the following code:
Dim strCriteria As String
strCriteria = "SELECT [PkgSize] & chr$(32) & [PkgUnit] AS Pkg, tblProducts.ProductID, tblProducts.ProductPrintName, tblProducts.Grade, " _
& " tblCustomers.CompanyName, tblOrderDetails.ODEPriority, chr$(33) & chr$(70) & [tblProducts].[ProductID] & [tblCustomers].[ID] & chr$(33)as Expr1" _
& " FROM tblCustomers INNER JOIN (tblOrders INNER JOIN (tblProducts INNER JOIN tblOrderDetails ON " _
& " tblProducts.ProductID = tblOrderDetails.ODEProductFK) ON tblOrders.ORDOrderID = tblOrderDetails.ODEOrderID) ON " _
& " tblCustomers.ID = tblOrders.ORDCustomerID " _
& " WHERE (((tblProducts.ProductID)=[Forms]![frmInventoryTransfersManual]![cboTransferProductID]) " _
& " AND ((tblOrderDetails.ODEPriority)= " & varPriority & ") AND (([tblOrderDetails]![ODEQtyOrdered]-[tblOrderDetails]![ODEQtyProduced])>0))"
DoCmd.OpenReport "rptProductPaperLabelTCTRlogo", acViewPreview, , , , strCriteria
In the report I have:
Private Sub Report_Open(Cancel As Integer)
Me.RecordSource = Me.OpenArgs
End Sub
The various text boxes on the report use the following as their Control Sources: Grade, Expr1, ProductPrintName, Pkg, and CompanyName. (Expr1 produces a barcode for scanning the ticket.)
It works perfectly. However, I also need to print a label or, could be, a text box to form a border on the report. This label/textbox will be a color assigned to CompanyName. Therefore, the ticket can be seen quickly and know who the customer is just by knowing the color of this label/textbox.
Can anyone help me to change the color of a label/text box on the report dependent on the company name. We have about 20 different customers.
Add a color attribute to the table definition for the customer table. Add that color attribute to the recordsource of the form. Use that color to set the backcolor property of the control on the report in the appropriate eventhandler of the form, probably onChange?

Need to format code for querying with exact value from combo box

I have the following ms access query. I want it to query with the gender (female or male) selected from the combo box. At the moment, when I select "female", the query results also includes all male persons.
How should I format this code:
WHERE (((Q_Gender_Statistics.Year) Like [cboYear] & "*") AND ((Q_Gender_Statistics.Gender) Like "*" & [cboGender] & "*"))
Don't use Like if not needed:
" WHERE Q_Gender_Statistics.Year = " & [cboYear] & " AND Q_Gender_Statistics.Gender = '" & [cboGender] & "'"
If cboGender looks up "Male/Female" form an index value, try this:
" WHERE Q_Gender_Statistics.Year = " & [cboYear] & " AND Q_Gender_Statistics.Gender = " & [cboGender] & ""

using dlookup in a text box

I am trying to update the text box using dlookup when the user selects a item in combo box, I cannot do dlookup in control source of text box since i need to store it in the table, so I am doing it in default value.
Here is the dlookup but it is not working:
=DLookUp([Ground Clearance_inches],"tbVehicles","[Vehicle]=[Forms]![Cover Data]![vehicle]")
note that Ground Clearance_inches is number datatype in the tbVehicles table.
That should be
Private Sub vehicle_AfterUpdate()
''Vehicle is a number
Me.MyTextBoxNameHere = _
DLookUp("[Ground Clearance_inches]","tbVehicles","[Vehicle]= " _
& [Forms]![Cover Data]![vehicle])
''Vehicle is text
Me.MyTextBoxNameHere = _
DLookUp("[Ground Clearance_inches]","tbVehicles","[Vehicle]= '" _
& Replace([Forms]![Cover Data]![vehicle], "'","''") & "'")
''If the current form is [Cover Data] then
Me.MyTextBoxNameHere = _
DLookUp("[Ground Clearance_inches]","tbVehicles","[Vehicle]= '" _
& Replace(Me.[vehicle], "'","''") & "'")
End Sub
I'm not sure about it being in the Default Value, but in VBA the where clause would be "[Vehicle]=" & [Forms]![Cover Data]![vehicle]
Why not put it in the OnUpdate event of your combo box?

VBA code to create a report in MS Access

Can anyone help me create a code satatement that will allow me to create a report with the sQ statement below? The problem I'm having is that my form allows you to input a Cost center, but when I click on the command button to execute the code it asks me to input the cost center again before it shows me the report. I want to eliminate having to enter the cost center again and just take it from when it is enters on the form.
Private Sub CmdCC_Click()
Set mydb = CurrentDb
myCC = txtCC.Value
If IsNull(myCC) Or myCC = "" Then
MsgBox "Please enter a Cost Center!", vbCritical + vbOKOnly, pTitle
End If
sQ = "SELECT ZBASED.ACCT_UNIT, CenterName, ZBASED.ACCOUNT, ZBASED.ACCOUNT_DESC " & _
"FROM ZBASED, CCtable " & _
"WHERE (ZBASED.ACCT_UNIT = " & myCC & ") And (CenterNo = " & myCC & ") " & _
"ORDER BY ZBASED.ACCOUNT;"
If the report is already based on say,
SELECT ZBASED.ACCT_UNIT, CenterName,
ZBASED.ACCOUNT, ZBASED.ACCOUNT_DESC
FROM ZBASED, CCtable
(There is no point in using ORDER BY with a report, you must use the report's own Oder & Grouping properties)
You can use the Where argument of OpenReport:
DoCmd.OpenReport "ReportName", acViewPreview, , "ZBASED.ACCT_UNIT = " & myCC _
& " And CenterNo = " & myCC
All you have to do is reference the form you are calling the report from in the SQL, for example
SELECT foo FROM bar WHERE foo=[Forms]![frmReporting]![txtFoo]
You then have a button on frmFoo that opens the report, you can include some logic in before the docmd.OpenReport call to validate the input i.e. make sure they have entered a cost centre