Passing query result to a textbox control on an access form - ms-access

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.

Related

access combobox values not showing

I am trying to create a form with 3 combo boxes on which will run a report from existing data. The combo boxes will filter down based on the data chosen in the next level up combo box.
However I'm having issues with the Batch Number Combobox not populating it's list
Now this is my VBA script:
Private Sub Form_Load()
Me.cboDate.RowSource = ""
Me.cboBatchNo.RowSource = ""
End Sub
Private Sub cboBottleNo_AfterUpdate()
Dim sDateSource As String
sDateSource = "SELECT [tblNewCC].[Date] FROM [tblNewCC]" & _
" WHERE [tblNewCC].[BottleNo] = " & Me.cboBottleNo.Value
Me.cboDate.RowSource = sDateSource
Me.cboDate.Requery
End Sub
Private Sub cboDate_AfterUpdate()
Dim sBatchSource As String
sBatchSource = "SELECT [tblBatchTotals].[BatchNo] FROM [tblBatchTotals] INNER JOIN [tblNewCC] ON [tblBatchTotals].[RunNo]=[tblNewCC].[RunNo]" & _
" WHERE [tblNewCC].[BottleNo] = " & Me.cboBottleNo.Value & _
" AND [tblNewCC].[Date] = " & Me.cboDate.Value
Me.cboBatchNo.RowSource = sBatchSource
Me.cboBatchNo.Requery
End Sub
From what I can see this is working alright on the vba side as I can see it replacing the rowsource of the batch number combobox, and in datasheet view it is giving me results.
However the combobox isn't showing anything in it's list....
Unless I go in make a change and save the sql query again.
Any clues?
The last part of sBatchSource should be " AND [tblNewCC].[Date] = #" & Me.cboDate.Value & "#" As an aside, 'Date' is a terrible name for a field. It is a reserved word in Access and will eventually cause you problems. Here is a helpful link http://www.fontstuff.com/access/acctut15pfv.htm

filter continuous form using textbox

I need to let users filter a continuous form using values the user enters into a textbox. And the continuous form is also nested within a couple levels of navigation subforms. This sounds easy enough, but all the examples I find on the web use macros instead of vba.
I set up the structure and wrote an AfterUpdate procedure for a textbox txtFilter as follows:
Private Sub txtFilter_AfterUpdate()
Dim filterval As String
filterval = txtFilter.Value
With Forms!Main!NavigationSubform.Form!NavigationSubform.Form
.Filter = "LastName Like " & filterval
.FilterOn = True
End With
End Sub
I have played with different syntax, but none of it seems to work properly. Here is a link to download the relevant parts of the database from a file sharing site: http://jmp.sh/v/HGctZ4Ru74vDAjzN43Wq
Can anyone show me how to alter this so that users can use the textbox to filter the continuous form?
I got it to work using this: .Filter = "LastName Like """ & filterval & """"
Need those annoying String Identifiers even for strings sometimes.
Okay, To get the form to open with no records and then pull up just the records you (or the user) specifies is easiest with a bit of re-work.
(I'd recommend you working with a copy and not your original)
1:On your Continuous Form, remove the Recordsource; we're going to use Late Binding (Kinda)
2:Then delete the code under the txtFilter box, then delete the box itself.
3:Add a comboBox with something like this as the recordsource:
SELECT DISTINCT myTable.LastName FROM myTable ORDER BY myTable.LastName; (This will get you a unique list of last names so knowing how to spell the name will not be necessary, plus it assures at least one match)
4:In the After Update event of that combobox, add code like this:
Dim strSource As String
strSource = "SELECT mt.IntakeNumber, mt.ClientNumber, " & _
"mt.LastName, mt.FirstName, mt.ConsultationDate " & _
" FROM myTable mt " & _
"WHERE (mt.LastName)= '" & Me.cboFilter.Value & "'"
Me.RecordSource = strSource
Me.Requery
Obviously you'll need to change the table and field names as necessary, but hopefully you get the idea.
Option Compare Database
Option Explicit '<- always include this!!!!!
Private Sub txtFilter_AfterUpdate()
Dim strFilter As String
' only set Filter when text box contains something
' to search for ==> don't filter Null, empty string,
' or spaces
If Len(Trim(Me.txtFilter.Value & vbNullString)) > 0 Then
strFilter = "LastName Like '*" & _
Replace(Me.txtFilter.Value, "'", "''") & _
"*'"
' that Replace() prevents the procedure from breaking
' when the user enters a name with an apostrophe
' into the text box (O'Malley)
Debug.Print strFilter ' see what we built, Ctrl+g
Me.Filter = strFilter
Me.FilterOn = True
Else
' what should happen here?
' maybe just switch off the filter ...
Me.FilterOn = False
End If
End Sub

Add Criteria To VBA Code and Requery

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.

Filter in Access form

I have a form and I want it to be filtered just after it loads.
After I click on the form it should be able to load by filtering specific data.
I want it to filter by Program Nam and Year.
I have tried the following code but I keep getting syntax errors:
Private Sub Form_Load()
Combo5.Value = Form_0_Cover.Combo0
Combo7.Value = Form_0_Cover.Combo2
'Me.Filter = "[Program_Name]=" & Me.Combo7 & " AND [Budget_Year]='" & Me.Combo5 & ""
End Sub
I am not sure what the problem seems to be. I keep getting syntax error.
Try:
Me.Filter = "[Program_Name]='" & Me.Combo7 & "' AND [Budget_Year]=" & Me.Combo5
I suspect that program name is text and budget year is numeric. It is possible that the program name combo has an id as the bound column, in which case things might get a little more difficult, probably:
Me.Filter = "[Program_ID]=" & Me.Combo7 & " AND [Budget_Year]=" & Me.Combo5

Runtime query does not work.Why?

I have a Problem with my codes and it made me confuse.this is my scenario:
in my access Project I have a Form and subform that work together.I wanted to enhace my performance . I deceided to load my form and subforms in Runtime(from this article) .I put my query on my form code, in Form_load event like this:
Private Sub Form_load()
Me.RecordSource = "SELECT DISTINCTROW tb_bauteile.* " & _
"FROM tb_bauteile LEFT JOIN FehlerCodes_akt_Liste ON tb_bauteile.CDT = FehlerCodes_akt_Liste.CDT " & _
"WHERE (((FehlerCodes_akt_Liste.Steuergerät)='MEDC17'))" & _
"ORDER BY FehlerCodes_akt_Liste.Fehlerpfad;"
End Sub
but another problem occured with another controls in my form.when i click another control .this function should be run:
Private Sub subPfadFilter(Kombifeld As Variant, Obd As String)
Dim strKrit, strAuswahl, strSg As String
If (IsNull(Me.CDT) Or (Me.CDT = "")) Then
strAuswahl = "*"
Else
strAuswahl = Me.CDT
blnFiltOn = True
End If
.....
but it doesn't work and say me method or dataobjectt not found( without this if statement works the function works fine .the problem come from CDT ) if I put the query in Datasource in my form property that works properly
I cant understand the relation between these two things(putting the query on datasource of form property then working the if statement (CDT) good but when i put the query in Form load that does not work ) ,would you please help me if posible?have you any idee?
thank you so much for your helps
When using the "Me" reference, you need to ensure the code is behind the present form, you've mentioned that the Form_Load code is on there, but is the "subPfadFilter" somewhere else?
If so then it needs to either be moved on to this form, or change your reference to the forms name instead of "Me".
If it is on the form then the error seems to be complaining about the CDT item, you will need to confirm that this is the name of either a text field or a control on this form.
If it is then you can try inserting:
Me.Requery
After your SQL statement, this may wake up the forms reference to the object.
This bit is not essential but to avoid carrying out two tests on CDT and streamlining your code slightly I would amend to this:
If Me.CDT & "" = "" Then
strAuswahl = "*"
Else
strAuswahl = Me.CDT
blnFiltOn = True
End If
This captures both Is Null or "" in one shot.
UPDATE:
For an unbound form, you need to set your form's controls via VB as well as the forms recordsource e.g:
Private Sub Form_load()
Dim rs As Recordset
Set rs = CurrentDb.OpenRecordset("SELECT DISTINCTROW b.* " & _
"FROM tb_bauteile As b LEFT JOIN FehlerCodes_akt_Liste As f " & _
"ON b.CDT = f.CDT " & _
"WHERE f.Steuergerät = 'MEDC17' " & _
"ORDER BY f.Fehlerpfad;")
rs.MoveFirst
Me.CDT = rs!CDT
'Any other fields go here
rs.Close
Set rs = Nothing
End Sub
However if you are returning more than one record at a time then you will either need to stick with a bound form or perhaps use a listbox to display your information. Unbound forms do not use the Forms "Recordset" property.
Note: I have also 'Aliased' the table names in your SQL statement, this is a good practice to get into : )