MS Access combo box setting a record with two primary keys - ms-access

I have a database with 2 primary keys, one for a LINE NUMBER and one for PHASE of construction. The reason for this is that we have projects that may use the same Line Number but must track several Phases of the project entirely seperatly. What I have is a combo box that will drive the record information on a form. This works fine, but now when I have more than one phase it will only bring up the line's first phase and not the other 4 phases. When something other than phas one is picked it results the first phase information.
Is there a way to tie a combo box with 2 fields to select the proper record based on both fields picked?
Or maybe I need to rething the way the form is brought up... Is there a better way to do this?
Code used to select the record:
Sub SetFilter()
Dim LSQL As String
LSQL = "select * from tblLineData_Horizon"
LSQL = LSQL & " where lineno = '" & cboSelected & "'"
Form_frmHorizon_sub.RecordSource = LSQL
End Sub
Private Sub cboSelected_AfterUpdate()
'Call subroutine to set filter based on selected Line Number
SetFilter
End Sub
Private Sub Form_Open(Cancel As Integer)
'Call subroutine to set filter based on selected Line Number
SetFilter
End Sub

A basic idea, but you'll most likely want to tweak the behaviour a bit and have some more checks. When the form loads, you only have the ability to select LineNo. When cbxLineNo has a value in it, it enables cbxPhaseNo for selection and upon selection, it changes the RecordSource of your subform.
Private Sub cbxLineNo_AfterUpdate()
If IsNull(cbxLineNo) Then
cbxPhaseNo.Enabled = False
Else
cbxPhaseNo.Enabled = True
cbxPhaseNo.RowSource = "SELECT PhaseNo FROM tblLineData_Horizon WHERE LineNo = " & cbxLineNo & ";"
End If
End Sub
Private Sub cbxPhaseNo_AfterUpdate()
If IsNull(cbxPhaseNo) = False And IsNull(cbxLineNo) = False Then
tblLineData_Horizon_sub.Form.RecordSource = "SELECT * FROM tblLineData_Horizon WHERE LineNo = " & cbxLineNo & " AND PhaseNo = " & cbxPhaseNo & ";"
End If
End Sub
Private Sub Form_Load()
cbxLineNo.Enabled = True
cbxPhaseNo.Enabled = False
cbxLineNo.RowSource = "SELECT LineNo FROM tblLineData_Horizon GROUP BY LineNo;"
End Sub

You question is a little unclear, but you can create a combobox with more than one column, then your select statement would be:
where lineno = '" & cboSelected.Column(0) & "' And otherfield='"& cboSelected.Column(1)&"'"
Go to the query behind your combobox by clicking into its RowSource property and clicking the Build button (...). Once you've added the columns you need, bring up the Properties for the query and set Unique Values to Yes, so that it doesn't repeat the combinations of the fields.
You will also need to change other properties of the combobox: 'Column Count' and 'Column Widths'.

Related

Why does Access VBA dropdown method not work?

I'm using a text box to filter a combo box list in Access 2013. I put the filter code in the Form_Timer sub so as to give users time to type the entire filter string before applying the filter, and I invoke the timer from the text box Change sub. It works great except for one thing: I want the combo box list to drop down and display results, and it just won't work. However I put the exact same line of code in the GotFocus sub for the combo box, and that line works perfectly.
I also tried executing the filter code within the Change sub, just in case there was some weirdness regarding Form_Timer execution. Same result. Here is the code:
Private Sub cboCENamesMain_GotFocus()
Me.cboCENamesMain.Dropdown '<---This line works perfectly.
End Sub
Private Sub Form_Timer()
Dim strSQL As String
Me.TimerInterval = 0
Me.txtFilter.Value = Me.txtFilter.Text
Me.cboCENamesMain.SetFocus
strSQL = ""
strSQL = strSQL & "Select DISTINCT [CE ID] "
strSQL = strSQL & "From [tblMyTable] "
If Len(Me.txtFilter) > 0 Then
strSQL = strSQL & "Where [CE ID] Like ""*" & Me.txtFilter & "*"" "
End If
strSQL = strSQL & "Order By [CE ID];"
Me.cboCENamesMain.RowSource = strSQL
Me.cboCENamesMain.Dropdown '<---This line doesn't do what it's supposed to.
Me.txtFilter.SetFocus
Me.txtFilter.SelStart = Len(Me.txtFilter.Text)
Me.txtFilter.SelLength = 0
End Sub
Private Sub txtFilter_Change()
If Len(Me.txtFilter.Text) = 0 _
Or Len(Me.txtFilter.Text) > 2 Then
Me.TimerInterval = 500
End If
End Sub
I could use a list box instead of a combo box to allow users to see the results of their filter typing, but that would seriously detract from my form design. I have searched on Google and on StackOverflow, and have not found anybody else discussing this issue. Any ideas?
Here is my final code. This works just the way I want. The user types a few characters, and the timer automatically filters the combo box list down to items containing the typed string.
StackOverflow is a great place to file stuff like this - going from job to job it will save some time reinventing the wheel.
The extensive comments in the code are for the analysts who will need to maintain the applications after my contract ends. Neither of them has ever written VBA.
Private Sub cboCENamesMain_Change()
' Ordinarily the AfterUpdate event would automatically fire off the Change event.
' We set a flag to avoid that - otherwise the list would be filtered to only the selected record.
If booCancelChange = False Then
' Don't bother filtering the combo box list unless the filter text is null or longer than
' 1 character.
If Len(Me.cboCENamesMain.Text) <> 1 Then
' Set the Form_Timer event to fire off in 0.3 second to give them time to type a few characters.
Me.TimerInterval = 300
End If
Else
' Reset the flag, otherwise the Change code would stop working after the first record selection.
booCancelChange = False
End If
End Sub
Private Sub Form_Timer()
Dim strSQL As String
' Reset the timer to not fire off, so that it won't keep running without a change
' in the combo box.
Me.TimerInterval = 0
' If they have tabbed out of the combo box after selecting an item, we don't want to
' do this. It's unnecessary and it throws errors from references to the control's
' properties when focus is no longer on the control.
If Screen.ActiveControl.Name = Me.cboCENamesMain.Name Then
' Create a SQL filter for the combo box using the entered text.
strSQL = ""
strSQL = strSQL & "Select DISTINCT [CE ID] "
strSQL = strSQL & "From [Covered Entities] "
If Len(Me.cboCENamesMain.Text) > 0 Then
strSQL = strSQL & "Where [CE ID] Like ""*" & Me.cboCENamesMain.Text & "*"" "
End If
strSQL = strSQL & "Order By [CE ID];"
' Apply the filter.
Me.cboCENamesMain.RowSource = strSQL
Me.txtRowCount = Me.cboCENamesMain.ListCount
' Drop down the combo list so they can see the results of their filter text.
Me.cboCENamesMain.Dropdown
End If
End Sub

Repopulating subform Recordset prompts for query parameters

I have a Continuous subform that filters data based on a series of queries. I then need to populate a subform that I have placed in the footer based on some data, and I have crafted a query to accomplish this.
Private Sub UpdateXXXXX_Info()
If (Me.FormFooter.Visible <> True) Then
Me.FormFooter.Visible = True
End If
MsgBox "query start"
LabelXXXXX_.Caption = "XXXXX for: " & Me.[XXXXX__NAME] & " " & Me.[XXXXX__NAME] & " " & Me.[XXXXX__CATEGORY_NAME]
With CurrentDb.QueryDefs("XXXXX_Query")
.Parameters("XXXXXParam") = Me.[XXXXX_NAME]
.Parameters("XXXXXCategoryParam") = Me.[XXXXX_CATEGORY_NAME]
Set Me.XXXXX__Subform.Form.Recordset = .OpenRecordset
.Close
MsgBox "query complete"
End With
End Sub
Which gets called from Click events tied to each of the controls on the continuous form. This works great, and filters as I expect.
Due to the nature of the continuous form (which itself is a subform of another form), the entire continuous form may need to be refreshed:
Private Sub Form_Current()
Me.FormFooter.Visible = False
End Sub
Which is causing an issue when refreshing the data. It works as-expected the first time around, however clicking on another record in the top continuous form causes the subform to prompt for query parameters. Based on testing, it is doing this when setting Me.FormFooter.Visible = True; although not setting Visible = False in the Current event circumvents this (which produces other, undesirable behavior).
Access will not let me .Close the Recordset, or otherwise set it to a blank or null value. I also cannot find any way to set the parameters for a Requery. The amount of data being presented also prohibits me from pulling the full table and applying a filter instead.
How can I prevent this behavior?
Edit: For clarification, I must use stored queries as a business requirement.
Use an SQL statement to (re)create the recordsource of the subform instead.
SQL = "SELECT * FROM xQuery WHERE xParam = '" & me!Name & "'" AND " & _
"xCategoryParam = '" & me!CategoryName & """
me!subform.form.recordsource = SQL
This eliminates any requirements to open/close the recordset, and avoids having to change your original query definition. (i.e. create your parameterless query, and the WHERE command in the SQL does the heavy lifting for you.
Aha!
geeFlo's answer pointed me to the RecordSource property of the form. Playing around with that, I was able to come up with the following:
Private Sub ClearXXXXXInfo()
If Me.FormFooter.Visible = True Then
Me.XXXXX_Subform.Form.RecordSource = ""
Me.FormFooter.Visible = False
End If
End Sub
Private Sub Form_Current()
ClearXXXXXInfo
End Sub

Replacing Multi-Value Fields With A Join Table

I'm moving on from multi-value fields due to my conversion to SQL Server for the back end. Unfortunately, I can't figure out how to replace it.
What I've done is created a many to many relationship between my "Opportunities" and "Purpose" tables utilizing a join table (one to many on each). This part was easy.
What I don't understand is how to then create a dropdown listbox (with check boxes to select the options) for the purpose.
I've found resources online pointing to the idea that I will need to use VBA, but have yet to find any actual examples. Is anyone familiar with how to do this?
Thanks in advance.
I had to do this for exactly the same reason; moving to a SQL Server back-end. My solution keeps the checkbox functionality but doesn't have them in a drop-down. You can put the check-boxes in a sub-form (that looks like a drop-down?) if you don't want a bunch of boxes on your main form.
For each checkbox you want to test for whether a record in the related table exists and add it if it doesn't when the box is checked, and the reverse when it's unchecked. The check-boxes must be unbound for this to work.
Here's the add/remove code. It goes in the "After Update" event:
Private Sub cb_1_AfterUpdate()
If Me.cb_1 = True Then
If DCount("Parent_ID", "Checkbox_records", "Parent_ID = " & Me.P_ID & "and Checked_box_num = 1") = 0 Then
strSQL = "INSERT INTO Checkbox_records (Parent_ID, Checked_box_num) VALUES (" & Me.P_ID & "," & "1)"
CurrentDb.Execute strSQL, dbFailOnError
End If
End If
If Me.cb_1 = False Then
If DCount("Parent_ID", "Checkbox_records", "Parent_ID = " & Me.P_ID & "and Checked_box_num = 1") > 0 Then
strSQL = "DELETE FROM Checkbox_records WHERE Parent_ID = " & Me.P_ID & " and Checked_box_num = 1"
CurrentDb.Execute strSQL, dbFailOnError
End If
End If
End Sub
The problem with unbound boxes is that they don't change when you switch records. So you have to set the boxes to reflect the data state when you change records, which you can do in the form's "On Current" event:
Private Sub Form_Current()
If DCount("Parent_ID", "Checkbox_records", "Parent_ID = " & Me.P_ID & "and Checked_box_num = 1") > 0 Then
Me.cb_1 = True
Else
Me.cb_1 = False
End If
End Sub
The thing I don't like about this solution is that you have to duplicate the code for each box, but it runs smoothly so it's livable.

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.

MS Access 03 Query using Yes/No Data Type

I have a table in Access..
AccessKey
AccessCardID
Distributed (yes/no)
On my form is a combo box with 10 Access Cards. I need help setting up a query where if guest A gets Access Card #1, the user will only be able to choose from Access Cards #2-#10, for guest B, c, and so on, until guest A returns Access Card #1.
so far the the query i have is
SELECT AccessCardID
FROM AccessKey
WHERE Distributed = False;
Here is my new code for After update
Private Sub AccessKeyNo_AfterUpdate()
If MsgBox("Do you want to assign Access Key" & Me.AccessKeyNo & "?", _
vbYesNo) = vbYes Then
Me.GuestAccessKeyID = Me.AccessKeyNo
Me.MyCheckBox = Not IsNull(Me.GuestAccessKeyID)
Me.AccessKeyNo.Requery
End If
End Sub
My new query
SELECT AccessKey.AccessKeyID
FROM AccessKey LEFT JOIN Guest ON AccessKey.AccessKeyID=Guest.GuestAccessKeyID
WHERE (((Guest.GuestAccessKeyID) Is Null));
And the On current for the form
Private Sub Form_Current()
Me.MyCheckBox = Not IsNull(Me.GuestAccessKeyID)
If IsNull(Me![GuestID]) Then
DoCmd.GoToControl "GuestFirstName"
End If
End Sub
Why don't you set Distributed to False in the above query so that you get the list of cards which have not been distributed yet.
If you do not like writing code, you can use a subform and set the record source to your table, AccessKey. Alternatively you can write a little code, say:
Private Sub NameOfComboHere_AfterUpdate()
Dim db As Database
Dim strSQL As String
Set db = CurrentDb
If MsgBox("Do you want to assign " & Me.NameOfComboHere & "?", _
vbYesNo) = vbYes Then
strSQL = "UPDATE AccessKey SET Distributed = True " _
& "WHERE AccessCardID = " & Me.NameOfComboHere
db.Execute strSQL, dbFailOnError
If db.RecordsAffected = 1 Then
MsgBox Me.NameOfComboHere & " has been assigned."
End If
End If
Me.NameOfComboHere.Requery
End Sub
There are a few notes. This assumes that the combobox has a bound column of AccessCardID and that AccessCardID is numeric. It also assumes that you have a reference to Microsoft DAO 3.x Object library. A subform may be the best bet.
EDIT based on Comments
Let us say you have a card id in the guest table, first, add a little code to the Current event for the form:
Me.MyCheckBox = Not IsNull(Me.GuestAccessCard)
This will set the checkbox to false if there is no card id and to true if there is an id.
You will need to change the query for the combobox to:
SELECT AccessCardID
FROM AccessKey
LEFT JOIN Guests
ON AccessKey.AccessCardID = Guest.GuestAccessCard
WHERE Guest.GuestAccessCard Is Null
Then the After Update event would run:
Private Sub NameOfComboHere_AfterUpdate()
If MsgBox("Do you want to assign " & Me.NameOfComboHere & "?", _
vbYesNo) = vbYes Then
Me.GuestAccessCard = Me.NameOfComboHere
Me.MyCheckBox = Not IsNull(Me.GuestAccessCard)
''The data will have to be saved for the
''combo to update with the new data
If Me.Dirty Then Me.Dirty = False
Me.NameOfComboHere.Requery
End If
End Sub
And do not forget to set the GuestAccessCard to Null when the checkbox is unticked and requery the combo.
I may be completely misunderstanding the situation here, but it seems to me that you don't want to have a Boolean field in the table with your cards to indicate if it's checked out -- all you need is a field in your Guests table where you enter the card number. Then you can tell which cards are available with a left join against the card numbers that are in use:
SELECT Cards.CardNumber
FROM Cards LEFT JOIN Guests ON Cards.CardNumber = Guests.CardNumber
WHERE Guests.CardNumber Is Null
This would mean that there's only ever one place where the information is stored (in the Guests table). You could also put a unique index on the CardNumber field in the Guests table (allow Nulls) so you could never assign the same card to two guests.
This may be the way you are doing things, or the way Remou has suggested, but I got too confused by all the convoluted back and forth!