using dlookup in a text box - ms-access

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?

Related

Convert Access SQL To VBA

I am in need of converting this Access SQL Query to a VBA Query ->
SELECT informationTable.userID,
ConcatRelated('itemSold','[informationTable]',"userID='" & [userID] & "'") AS NameOfItemSold
FROM informationTable
GROUP BY informationTable.userID;
I tried ths VBA
DoCmd.RunSQL ("SELECT informationTable.userID,
ConcatRelated('itemsold','[informationTable]','userID= ' & Chr(34) & [userID] & Chr(34) & ') AS NameOfItemSold
Into CRInformationTable
FROM informationTable
GROUP BY informationTable.userID;")
but I get an error of
A RunSQL action requires an argument consisiting of an SQL statement
I did some testing. Assuming userID is number type field, see if this works for you:
DoCmd.RunSQL ("SELECT DISTINCT informationTable.userID, " & _
"ConcatRelated('itemsold','[informationTable]','userID=' & [userID]) AS NameOfItemSold " & _
"INTO CRInformationTable FROM informationTable;")
If userID is text type:
"ConcatRelated('itemsold','[informationTable]','userID=" & Chr(34) & "' & [userID] & '" & Chr(34) & "') AS NameOfItemSold " & _
Instead of Chr(34):
"ConcatRelated('itemsold','[informationTable]','userID=""' & [userID] & '""') AS NameOfItemSold " & _
I've used this nifty tool many times with great success.
Creating the form
The form just needs two text boxes, and a command button. SQL statements can be quite long, so you put the text boxes on different pages of a tab control.
Create a new form (in design view.)
Add a tab control.
In the first page of the tab control, add a unbound text box.
Set its Name property to txtSql.
Increase its Height and Width so you can see many long lines at once.
In the second page of the tab control, add another unbound text box.
Name it txtVBA, and increase its height and width.
Above the tab control, add a command button.
Name it cmdSql2Vba.
Set its On Click property to [Event Procedure].
Click the Build button (...) beside this property.
When Access opens the code window, set up the code like this:
Private Sub cmdSql2Vba_Click()
Dim strSql As String
'Purpose: Convert a SQL statement into a string to paste into VBA code.
Const strcLineEnd = " "" & vbCrLf & _" & vbCrLf & """"
If IsNull(Me.txtSQL) Then
Beep
Else
strSql = Me.txtSQL
strSql = Replace(strSql, """", """""") 'Double up any quotes.
strSql = Replace(strSql, vbCrLf, strcLineEnd)
strSql = "strSql = """ & strSql & """"
Me.txtVBA = strSql
Me.txtVBA.SetFocus
RunCommand acCmdCopy
End If
End Sub
Using the form
Open your query in SQL View, and copy the SQL statement to clipboard (Ctrl+C.)
Paste into the first text box (Ctrl+V.)
Click the button.
Paste into a new line in your VBA procedure (Ctrl+V.)
http://allenbrowne.com/ser-71.html

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?

UPDATE SET WHERE Partially working on listbox - Microsoft Access VBA

I'm totally stumped at this one.. Skip to bottom to read problem
Here's my listbox (DocumentList) that takes the fields from the 'Documents' Table:
Document Name Status Notes Consultation Notes
Doc A Started Document Started Aim to process on 05/05/16
Doc B Processing Document Processing Aim to complete on 05/05/16
Doc C Complete Complete on 01/01/16 N/A
I have the onclick event set so that when you select a row from the listbox, it assigns each field to a text box/Combobox.
textboxes/Combobox names:
txtDocument
StatusCombo
txtNotes
txtConNotes
code for each one in 'DocumentList' click event:
Private Sub DocumentList_Click()
txtDocument = DocumentList.Column(0)
StatusCombo = DocumentList.Column(1)
txtNotes = DocumentList.Column(2)
txtConNotes = DocumentList.Column(3)
After the data is assigned to them from the listbox, you can edit it. I have an update button, which when pressed will replace everything in the database with everything in the textboxes/Combobox. The listbox is then re-queried and displays the updated data.
Heres the code for my update button:
Private Sub UpdateButton_Click()
CurrentDb.Execute "UPDATE [Documents] " & _
"SET [Document Name] = '" & Me.txtDocument & "'" & _
", [Status] = '" & StatusCombo.Value & "'" & _
", [Notes] = '" & Me.txtNotes & "'" & _
", [Consultation Notes] = '" & Me.txtConNotes & "'" & _
"WHERE [Document Name] = '" & DocumentList.Column(0) & "'" & _
"AND [Status] = '" & DocumentList.Column(1) & "'" & _
"AND [Notes] = '" & DocumentList.Column(2) & "'" & _
"AND [Consultation Notes] = '" & DocumentList.Column(3) & "'"
DocumentList.Requery
End Sub
My problem is the code only works on 2 out of 3 of the documents. All aspects of the code work, but only on some of the documents. This doesn't make any sense to me. At first I thought it may be a spelling error, but even if it was, none of the documents should get updated.. But some of them do, 1 doesn't..
Any ideas why this code updates some documents, but doesn't update others?
Nothing is updated when [Documents].[Consultation Notes] is Null, because the WHERE clause targets an empty string instead ... "'" & DocumentList.Column(3) & "'" ... so no matching row is found.
The task would be simpler if you add an autonumber primary key, ID, to the [Documents] table. Then include ID in the list box Row Source, and use that value in the WHERE clause to target the row you want to update. (The ID column doesn't have to be visible in the list box; you can set its column width property to zero.)
Then your WHERE clause can be much simpler: just target the record whose ID matches the ID column value of the selected list box row. That strategy would also avoid the complication of "Null is never equal to anything, not even another Null".
Finally, consider a parameter query for the UPDATE instead of concatenating values into a string variable.

Open form in access using criteria?

Is it possible to open a form in MS ACCESS 2007 using a where clause which consists of "and".
Suppose I want to open a form based on two criteria, i.e. I have a dropdown on a form that is used to open a form on the basis of the value selected but this dropdown looks up the selected value in another dropdown and so the Open form criteria should should include the selections in both the dropdowns.Is it possible to do so?
Is the following correct?
DoCmd.OpenForm Me.Name, , , "[A_PRIORITY] = '" & _
cboPrior & "'" AND "[A_LOCATION] = '" & _
cboLocate & "'"
You have too many quotes, it should look like:
DoCmd.OpenForm Me.Name, , , "[A_PRIORITY] = '" & _
cboPrior & "' AND [A_LOCATION] = '" & _
cboLocate & "'"
All commands and keywords are inside the main quotes, only variables are outside.

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