I'm using an On-click method on a button to pass the following
Private Sub cmdNewEnquiry_Click()
Call Command29_Click
DoCmd.RunSQL "INSERT INTO tblEnquiry(CustomerID) Values('" & CustomerID & "')"
DoCmd.OpenForm "frmEnquiry", acNormal, , "CustomerID = " & CustomerID
End Sub
But whenever it passes the CustomerID into the next form, EnquiryID isn't the newest it could be maybe it shows a record that's 1 before this one. I'm then having to click through the records to find the newest Enquiry.
Is there a way I can pass this data through and make sure it displays the most up-to-date record?
You have to change the RecordSource query in the form "frmEnquiry" by ordering the data for newest first. Or use the property field OrderBy in the form "frmEnquiry". Not knowing the query-fields of "frmEnquiry", I can not give you a more precise answer.
The part "CustomerID = " & CustomerID in the doCmd.OpenForm sets a filter for this CustomerID, but does not order anything.
Related
I am trying to filter the report based on a value in combo box on a form.
Private Sub OpenReport_Click()
DoCmd.OpenReport "Warehouse Stock Summary", acViewReport, "[ProductName] =" & Me.Combo202
End Sub
Where,
Warehouse Stock Summary is the name of the report,
Product Name is the column in the report based on whcih I want to filter the report.
Combo202 contains the distinct values for ProductName
Can anyone please help, where I am going wrong? The code above is not filtering the report. It will just give the results for all values.
Thanks,
Rohan
Try with quotes around the text value:
Private Sub OpenReport_Click()
DoCmd.OpenReport "Warehouse Stock Summary", acViewReport, "[ProductName] = '" & Me!Combo202 & "'"
End Sub
Also, rename Combo202 to something meaningful.
I have a form control (address) that uses Dlookup to call info from a table "database" but the form is bound to table "Tracker". The dlookup is based on another control on the same form - "Name" I need the form to record this dlookup control, along with other controls that are bound to "tracker" as new recordto the table "tracker."
My failed attempts:
Using the default value property to assign the recalled data from the dlookup to another text box which would be bound to "tracker" This simply does not work for some reason. Perhaps I am missing something that tells this control "Address" to update upon selecting the correct "name?"
Code:
Private Sub SubmitReferral_Click()
On Error GoTo Err_SubmitReferral_Click
DoCmd.GoToRecord , , acNewRec
Exit_SubmitReferral_Click:
Exit Sub
Err_SubmitReferral_Click:
MsgBox Err.Description
Resume Exit_SubmitReferral_Click
End Sub
I also tried this - to assign the data - but the data from the dlookup in control "Address1" is not transferring/copying to control "Address2"
Private Sub Combo276_OnUpdate()
OnUpdate ([Address2].Value = [Address1].Value)
End Sub
Help or suggestions?
PS - I have tried to Edit per request to be as specific as possible, and to follow proper board etiquette.
Still unsure of your field names, etc., but the following is an example you can modify. Change 'tblEmployee' to 'database'.
I must state that if you are just starting out with developing in Access (or VBA) that you should never use names that are reserved words, or that can be misleading. Your table named 'database' is ok if named 'tblDatabase'.
Option Compare Database
option Explicit
Private Sub cmdInsert_Click()
Dim strSQL As String
Dim i As Integer
Debug.Print "cmdInsert; "
i = MsgBox("Do you want to add 1 row for Employee ID: " & Me.EmpID & " to table 'tracker'?", vbYesNo, "Confirm Add")
If i = vbNo Then
Exit Sub
End If
DoCmd.SetWarnings True
strSQL = "INSERT INTO tracker ( FirstName, LastName, Add1, City, St, Zip ) " & _
"SELECT tblEmployee.FirstName, tblEmployee.LastName, tblEmployee.Add1, tblEmployee.City, tblEmployee.St, tblEmployee.Zip " & _
"FROM tblEmployee " & _
"WHERE (((tblEmployee.EmpID)=" & Me.EmpID & "));"
DoCmd.RunSQL strSQL
End Sub
Thanks for the help - I solved my concern by hiding the fields that contain the dlookup, and putting code behind a button that copies the information to fields that are bound and therefore will record to the table "tracker"
I need help with a textbox field on an Access 2007 form. I'm trying to insert the result of a query into the text box control on the form. This is used soley as information for the user. The form supplies the query with parameters to get the value. The query works fine and returns the correct result. What I can't seem to figure out is how to pass the query result to the textbox. I’ve tried several different ways but with no luck.
(PS> I know a combo box can do a lookup, however I don’t want the user to have to click the dropdown just to select the value as there can only ever be one value result from the query.) I'm open to suggestions as I'm not a programmer or DB Admin, but I've taking a few classes on Access (enough to be dangerous).
Private Sub cbo3_Change()
Me.tbx2 = ("SELECT tbl_Billing.Savings_b FROM tbl_Billing GROUP BY tbl_Billing.UBI_b, tbl_Billing.TaxYr_b, tbl_Billing.TaxPrg_b, tbl_Billing.Savings_b HAVING (((tbl_Billing.UBI_b)=forms!f1_UpBilled!cbo1) And ((tbl_Billing.TaxYr_b)=forms!f1_Upbilled!cbo2) And ((tbl_Billing.TaxPrg_b)=forms!f1_UpBilled!cbo3));")
End Sub
If you wish to do this in run time, you need to do the following, I take the controls you are referring to in this is on the same form.
The very simple and straight forward way to get it done is as follows,
Private Sub cbo3_Change()
Dim tmpRS As DAO.Recordset
Set tmpRS = CurrentDb.OpenRecordset("SELECT tbl_Billing.Savings_b FROM tbl_Billing GROUP BY " & _
"tbl_Billing.UBI_b, tbl_Billing.TaxYr_b, tbl_Billing.TaxPrg_b, " & _
"tbl_Billing.Savings_b HAVING ((tbl_Billing.UBI_b = '" & Me.cbo1 & "') And (tbl_Billing.TaxYr_b = '" & Me.cbo2 & "') " & _
"And (tbl_Billing.TaxPrg_b = '" & Me.cbo3 & "'))")
If tmpRS.RecordCount > 0 Then
Me.tbx2 = tmpRS.Fields(0)
Else
Me.tbx2 = 0
End If
Set tmpRS = Nothing
End Sub
Just note, I have implied all your combo boxes are returning String and the field you are comparing against are Text type. If that is not the case, you need to make changes accordingly.
I have a list box that displays my entire Access databases records with a brief descriptions in additional columns. The first column (0) has the record identifier (PART NUMBER) which is the primary key in the table. I am trying to get the double click event to open the "SETUP SHEET DATA ENTRY" Form to the specific row in the list box. The part number is classified as text and not numerical. I don't know what the problem is and any advice would help.
List box name = Listallpart
Primary key in table name = PART NUMBER
Table name = Setup Sheet History
Form name = Setup Sheet Data Entry
Private Sub Listallpart_DblClick(Cancel As Integer)
DoCmd.OpenForm "SETUP SHEET DATA ENTRY", , , "[PART NUMBER] = " & Me.Listallpart.Column(0).Value
End Sub
I have another question very similar....
Private Sub Listallpart_DblClick(Cancel As Integer)
Dim strpn As String
Dim strco As String
strpn = Me.Listallpart.Column(0)
strco = Me.Listallpart.Column(1)
DoCmd.OpenForm "SETUP SHEET DATA ENTRY", , , ("[PART NUMBER] = '" & Me.Listallpart.Column(0) & "'" And "[CURRENTOPERATION] ='" & Me.Listallpart.Column(1) & "'")
End Sub
The code above is for another mod to the code. Every record has these 2 required unique fields. I am getting a "run time error "13" mismatch type". I added the two variables in the beginning to make sure the values are correct. I inserted break points and confirmed the values. The data is numerical.
Since [PART NUMBER] is a text field, add quotes around the list box value in your OpenForm statement.
DoCmd.OpenForm "SETUP SHEET DATA ENTRY", , , "[PART NUMBER] = '" & Me.Listallpart.Column(0).Value & "'"
Your second question is the inverse of your first question; seeing as [CURRENTOPERATION] is numeric you don't need to add the quotes to this in your OpenForm
DoCmd.OpenForm "SETUP SHEET DATA ENTRY", , , ("[PART NUMBER] = '" & Me.Listallpart.Column(0) & "'" And "[CURRENTOPERATION] =" & Me.Listallpart.Column(1))
So the following VBA code I have will requery the form based on the Combo1 value equal to January. Eventually, I'll have X number of years in the Combo value and only want to display all records based on each year.
Is it possible to add additional VBA code to use the Criteria from the query instead of having X amount of queries and requery them based on the year.
Private Sub Combo1_AfterUpdate()
If Combo1.Value = "2013" Then
[Form_Main Form].RecordSource = "Main_Form_Query"
[Form_Main Form].Requery
End If
End Sub
You can construct the query in runtime (remember: a query object in access is just an sql statement)
So, let's asume that your data is stored in a table called tblMyData that has columns called month (String, month name) and year (Numeric, Integer) (just examples). You can generate the SQL query in VBA this way:
...
Dim strSQL as String
...
strSQL = "SELECT * FROM tblMyData " & _
"WHERE month='" & ComboMonth.Value & "' " & _
" AND year=" & ComboYear.Value ";"
...
[Form_Main Form].RecordSource = strSQL
[Form_Main Form].Requery
...
Hope this helps you.
Open the form in a different way.
DoCmd.OpenForm "MyForm",,,"MyYear=2013"
Or
DoCmd.OpenForm "MyForm",,,"MyYear=" & Me.MyCombo
Assuming that MyYear in the table or query is numeric. If it is not, you can use single quotes.
The general idea is that you use a form with all records, and use the Where argument of OpenForm to specify the records that should be shown. You can do the same with reports.
I have only a vague idea about what you're trying to accomplish. It might help to show us the SQL from Main_Form_Query. Meanwhile, consider whether you can use the form's .Filter property to do what you need.
If the form's Record Source includes a text field named fiscal_year, you could filter the rows displayed based on the year selected in your combo box.
Private Sub Combo1_AfterUpdate()
' .Value is the default property, so not required after Me.Combo
Me.Filter = "fiscal_year = '" & Me.Combo1 & "'"
Me.FilterOn = True
End Sub
Yet another approach could be to reference the combo value in Main_Form_Query.
WHERE
Forms!YourForm!Combo1 Is Null
OR some_field = Forms!YourForm!Combo1
Then do Me.Requery in the combo's After Update event.