Open form in access using criteria? - ms-access

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.

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

DoCmd.SearchForRecord with multiple criteria in Access 2013

I have a form pulling data off an SQL Server table. The form has two unbound comboboxes. Users first select from comboName, then comboDate, then the form will retrieve the record associated with the values.
'comboDate AfterUpdate VBA macro
DoCmd.SearchForRecord , "", acFirst, "[Name] = " & "'" & comboName.Value & "'" & _
" and [Date] = " & "'" & Format(comboDate, "yyyy-mm-dd") & "'"
If I use Name = comboName.Value alone it will retrieve the first record for that name, but if I add the Date criteria, or use the Date criteria without Name, the combobox will no longer retrieve the record. It just stays on the current one. I have already converted the Access date format to match the SQL Server. What else needs to be done?
Access needs # symbols for it to recognize it as a date. It works when I change the query to this.
'comboDate AfterUpdate VBA macro
DoCmd.SearchForRecord , "", acFirst, "[Name] = " & "'" & comboName.Value & "'" & _
" and [Date] = " & "#" & Format(comboDate, "yyyy-mm-dd") & "#"

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?

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