I have a small, but elusive problem:
In MS Access, I have a single result form. On this form I have a list. This list is a subselect based on the ID of the main form. How can I capture the row change, and so update my list?
I am NOT looking for Insert / Update Statements! I have them galore, and they work fine, but do not help me unless each row is updated before / as soon as changing rows.
In my example, the list is Genres for a selected Film.
Private Sub film_name_AfterUpdate()
Dim strNewRecord As String
Dim film_id As Integer
If IsNull(Forms!Films!film_id) Then
GoTo cmdNoRecord_Error
Else:
film_id = Forms!Films!film_id
strNewRecord = "SELECT qry_film_genres.gen_name FROM qry_film_genres " _
& " WHERE film_id = " _
& film_id & " ORDER BY qry_film_genres.gen_name"
Me.Genre_List.RowSource = strNewRecord
End If
Exit Sub
cmdNoRecord_Error:
MsgBox "Please Select a Film."
Exit Sub
End Sub
You just need to use the film_id Change event. I'm assuming the 'film_id' field is on the parent form, and a subform is where the 'Genre_List' control lives.
EG:
Private Sub film_id_Change()
If Not IsNull(Me.film_id) Then
Me.CHILDFORM.Genre_List.RowSource = "SELECT qry_film_genres.gen_name FROM qry_film_genres " _
& " WHERE film_id = " & Me.film_id & " ORDER BY qry_film_genres.gen_name"
End If
End Sub
Related
I set-up a form with with project details and want to implement several drop-down options to filter project properties. These are "project-name", "customer" and "product". I want to make them dependent on each other. So when I select a particular customer, I only want to be able to choose from products which are related to the customer.
So far I was able to set-up each drop-down for each project property but they are not dependet of each other, but always show all available options. Can someone help me out with dependent drop-down fields?
Selection fields as they are now
Edit: Table layout upon request:
Rows/format
-Project ID/Number
-Customer/String
-Product/String
-Project-name/String
-Comment/String
-Feature 1/String
One way of doing this to to use "cascading" combo boxes, so the user moves from the top combo box towards the bottom one, each time narrowing the range available to them.
Your VBA code would look something like this:
Private Sub cboCustomer_AfterUpdate()
Dim strSQL As String
If Len(Me!cboCustomer) > 0 Then
strSQL = "SELECT DISTINCT Product FROM tblProject " _
& " WHERE Customer='" & Me!cboCustomer & "' " _
& " ORDER BY Product ASC;"
Me!cboProduct.RowSource = strSQL
Else
strSQL = "SELECT DISTINCT Product FROM tblProject ORDER BY Product ASC;"
End If
End Sub
Private Sub cboProject_AfterUpdate()
Dim strSQL As String
If Len(Me!cboProject) > 0 Then
strSQL = "SELECT DISTINCT Customer FROM tblProject " _
& " WHERE ProjectID=" & Me!cboProject _
& " ORDER BY Customer ASC;"
Me!cboCustomer.RowSource = strSQL
strSQL = "SELECT DISTINCT Product FROM tblProject " _
& " WHERE ProjectID=" & Me!cboProject _
& " ORDER BY Product ASC;"
Me!cboProduct.RowSource = strSQL
Else
Me!cboCustomer.RowSource = "SELECT DISTINCT Customer FROM tblProject ORDER BY Customer ASC;"
Me!cboProduct.RowSource = "SELECT DISTINCT Product FROM tblProject ORDER BY Product ASC;"
End If
End Sub
Regards,
I am creating a database to manage the flow of people in and out of an incident system. My idea is to use the DLookup function to allow me to identify a null value in the checkout date. This would allow me to prevent someone from jumping incidents without signing out first.
If it is possible I would like to get the EVENT ID and the COMMAND POST ID so I can create an error message to tell me the incident that is member is still attached to.
'***Section 1 - Ensure member is Checked out and DEMOBed from previous incidents
'******Variables
'*********Variable to hold the Member ID
Dim id As Integer
'*********Variable to hold the checkout status
Dim checkout As String
'*********Variable to hold the Event ID
Dim eventID As Integer
'******Code Block 1 - Check for Null Values
id = Me.Text18
Forms![frm_ics238Table].Refresh
If Not IsNull(DLookup("[eventID]", "[frm_ics238Table]", "[checkoutDate] is Null And employeeID = '" & Me.Text18 & "'")) Then
MsgBox "y"
End If
End Sub
From your original question which right now it doesn't look like you've edited it to indicate the updated code and errors, you had:
id = Me.Text18
Forms![frm_ics238Table].Refresh
If Not IsNull(DLookup("[eventID]", "[frm_ics238Table]", "[checkoutDate] is Null And employeeID = '" & Me.Text18 & "'")) Then
MsgBox "y"
End If
I think these might help:
"[frm_ics238Table]" should be table:"[tbl_ics238Table]"
ID appears to be declared as an integer yet you're using it as a
string. If employeeID is actually a numeric field remove the quotes
around employeeID = '" & Me.Text18 & "'" It should look like this employeeID = " & ID & "
You can also try using IsNull([checkoutDate]) instead of
[checkoutDate] is Null
SO it might look like:
If Not IsNull(DLookup("[eventID]", "[tbl_ics238Table]", "IsNull([checkoutDate]) And (employeeID = " & ID & ")")) Then
MsgBox "y"
End If
Even better would be to put the criteria into a string variable so you could debug and test it first
Dim strCriteria as String
strCriteria = "IsNull([checkoutDate]) And (employeeID = " & ID & ")"
Debug.Print strCriteria
If Not IsNull(DLookup("[eventID]", "[tbl_ics238Table]", strCriteria)) Then
MsgBox "y"
End If
EDIT: PROOF OF WORKING CODE:
Not that I don't believe you but I don't believe that it doesn't work if what you've described matches my assumptions.
I tested successfully using assumptions that:
eventID - number field
checkoutDate - date/time field
employeeID - number field
My sample data table
Then I put a command button on a form to test the code:
Private Sub Command8_Click()
Dim strCriteria As String
Dim ID As Integer
ID = 4
strCriteria = "IsNull([checkoutDate]) And (employeeID = " & ID & ")"
Debug.Print strCriteria
If Not IsNull(DLookup("[eventID]", "[tbl_ics238Table]", strCriteria)) Then
MsgBox "y"
End If
End Sub
Finally - testing the button click resulted in:
In conclusion - if you're still getting an error then you can update
your question with BOTH your code AND your table design.
I have two multiple select list boxes "Animal" and "State". These two list boxes are dependent on each other. For example, if i select "in" and "mi" in the state list box, the animal list box will only show animals that are in "in" and "mi".
My issue is i have the rowsource set to the query below which is assigned to the "on click" event in the animal list box. When i click in the animal list box it runs the query but it no longer allows me to select any animals.
How do i change the list box to allow me to select animals in the animal list box?
Private Sub animal_Click()
Dim Q As QueryDef
Dim DB As Database
Dim Criteria As String
Dim ctl As Control
Dim Itm As Variant
Dim ctl2 As Control
Dim ctl3 As Control
' Use for dynamic SQL statement'
Dim strSQL As String
Set ctl2 = Me![State]
For Each Itm In ctl2.ItemsSelected
If Len(Criteria2) = 0 Then
Criteria2 = Chr(34) & ctl2.ItemData(Itm) & Chr(34)
Else
Criteria2 = Criteria2 & "," & Chr(34) & ctl2.ItemData(Itm) & Chr(34)
End If
Next Itm
' Modify the Query.
Set DB = CurrentDb()
Set Q = DB.QueryDefs("animal")
' Modify the Query.
Set DB = CurrentDb()
Set Q = DB.QueryDefs("animal")
strSQL = "Select distinct(animal) From [table1] Where [table1].[type] In (" & "'Animal')"
If (Len(Criteria2) <> 0) Then ' Append State Criteria
strSQL = strSQL & " AND [table1].[state] IN (" & Criteria2 & ")"
End If
Q.SQL = strSQL
Q.Close
' Run the query.
'DoCmd.OpenQuery "animalquery"
Me.Animal.RowSource = strSQL
End Sub
I think i solved for this.
I originally changed "Private Sub animal_Click()" to "Private Sub animal_dblClick()" but this was resulting in an event procedure error.
I then changed it to "Private Sub animal_Enter()". When i enter the list box it runs the query while still allowing me to select items in the list box.
However, if someone has a different solution or know how to use the double click method. that would be great!
I am struggling with something on my project and I wondered if anybody would be able to point me in the right direction.
An image should be able to explain the functionality easier than I can describe it:
Quite simply, I want users to be able to click on 'Add' and then double click on one of the specialities to assign it to that DJ.
When the user clicks on the save button, a new row to a table called 'tblDJSpecialitiy' for each speciality they have chosen needs to be created. Each row needs to contain the 'Dj_No' and the primary key of what ever the speciality is.
First of all I need to work on getting specialities list on the Edit DJ form to update to reflect the specialities that the user click on from the list of all specialities.
Here is the code I currently have for the specialities list on the Edit DJ form:
Public Sub displaySpecialities()
'for the selected member display current loans (including overdue loans)
'and loans which have been returned but for which fines are still outstanding
Dim RstSpecialities As DAO.Recordset
Dim strSQL, strSpecialityInformation As String
Dim strDJNo As String
Dim intNumberOfSpecialities, intIndex As Integer
strDJNo = txt_dj_no.Value
'call the function to clear the list
Call clearLst_Specialities
'create an sql query to find the specialities
strSQL = strSQL & "SELECT tblSpeciality.SpecialityName FROM tblSpeciality INNER JOIN tblDJSpeciality ON tblDJSpeciality.[FKSpecialityNo] = tblSpeciality.[SpecialityNo] WHERE tblDJSpeciality.FKDjNo = '" & strDJNo & "';"
'store the results of the query in the recordset
Set RstSpecialities = dbase.OpenRecordset(strSQL, dbOpenDynaset)
'calculate the number of specialities
If Not RstSpecialities.EOF Then
RstSpecialities.MoveLast
intNumberOfSpecialities = RstSpecialities.RecordCount
RstSpecialities.MoveFirst
End If
'add the details of each speciality to the list box
For intIndex = 0 To intNumberOfSpecialities - 1
strSpecialityInformation = RstSpecialities("SpecialityName")
lst_specialities.AddItem (strSpecialityInformation)
RstSpecialities.MoveNext
Next
'close the recordet
RstSpecialities.Close
End Sub
And here is the code which I currently have to populate the list of all specialities on the add form:
Public Sub displaySpecialities()
'list all specialities in the system
Dim RstSpecialities As DAO.Recordset
Dim strSQL, strSpecialityInformation As String
Dim intNumberOfSpecialities, intIndex As Integer
'call the function to clear the list
Call clearLst_Specialities
'create an sql query to find the specialities
strSQL = strSQL & "SELECT tblSpeciality.SpecialityName FROM tblSpeciality ORDER BY SpecialityName ;"
'store the results of the query in the recordset
Set RstSpecialities = dbase.OpenRecordset(strSQL, dbOpenDynaset)
'calculate the number of specialities
If Not RstSpecialities.EOF Then
RstSpecialities.MoveLast
intNumberOfSpecialities = RstSpecialities.RecordCount
RstSpecialities.MoveFirst
End If
'add the details of each speciality to the list box
For intIndex = 0 To intNumberOfSpecialities - 1
strSpecialityInformation = RstSpecialities("SpecialityName")
lst_speciality_add.AddItem (strSpecialityInformation)
RstSpecialities.MoveNext
Next
'close the recordet
RstSpecialities.Close
End Sub
I have been googling for a good while trying to work it - but without much luck. Thanks to anyone who replies.
EDIT:
This comes up twice - if I put in '11' (the DJ number) both times then I get a list up - but it shows like this:
this seems to be all of the records in my tblDJSpecialty
Below is some code that will add/delete specialties between the two list boxes. But first, a few explanations:
The listbox on the left (lst_specialities)is for the individual DJ specialties
The listbox on the right (lst_specialities_All) contains ALL specialties to choose from
tblSpeciality has two columns: (1) SpecialityNo, AutoNumber, PK; (2) SpecialityName Text
Table tblDJSpecialityID has 3 columns: (1) DJSpecialityID AutoNumber, PK; (2) DJ_FKDjNo LongInt (DJ's PK); (3) FKSpecialityNo, LongInt (PK of Speciality)
Change the names used to match your names.
The following is the rowsource SQL of the Preferences listbox 'lst_specialities'
SELECT tblDJSpecialitiy.DJSpecialityID, tblDJSpecialitiy.DJ_FKDjNo, tblDJSpecialitiy.FKSpecialityNo, tblSpeciality.SpecialityName FROM tblDJSpecialitiy
INNER JOIN tblSpeciality ON tblDJSpecialitiy.FKSpecialityNo = tblSpeciality.SpecialityNo
WHERE (((tblDJSpecialitiy.DJ_FKDjNo)=[Forms]![dj_edit]![txt_dj_no]));
The following is the rowsource of the listbox 'lst_specialities_All':
SELECT tblSpeciality.SpecialityNo, tblSpeciality.SpecialityName
FROM tblSpeciality ORDER BY tblSpeciality.SpecialityName;
The following is the code for adding / deleting. I named the command buttons cmdPlus and cmdMinus:
Option Compare Database
Option Explicit
Private Sub Form_Current()
Me.lst_specialities.Requery
End Sub
Private Sub cmdMinus_Click()
Dim strSQL As String
Dim i As Integer
If Me.lst_specialities.ItemsSelected.Count = 0 Then
MsgBox "You must select a specialty to remove from this DJ.", vbOKOnly, "No Specialty Selected"
Exit Sub
End If
For i = 0 To 3
Debug.Print i & vbTab & Me.lst_specialities.Column(i)
Next i
strSQL = "DELETE tblDJSpecialitiy.DJSpecialityID, tblDJSpecialitiy.FKSpecialityNo " & _
"FROM tblDJSpecialitiy " & _
"WHERE (((tblDJSpecialitiy.DJSpecialityID)=" & Me.lst_specialities.Column(0) & "));"
DoCmd.SetWarnings False
DoCmd.RunSQL strSQL
DoCmd.SetWarnings True
UnSelect_All
UnSelect_DJ
Me.lst_specialities.Requery
End Sub
Private Sub cmdPlus_Click()
Dim strSQL As String
Dim i As Integer
If Me.lst_specialities_All.ItemsSelected.Count = 0 Then
MsgBox "You must select a specialty to add to this DJ.", vbOKOnly, "No Specialty Selected"
Exit Sub
End If
For i = 0 To 3
Debug.Print i & vbTab & Me.lst_specialities_All.Column(i)
Next i
strSQL = "INSERT INTO tblDJSpecialitiy ( DJ_FKDjNo, FKSpecialityNo ) " & _
"SELECT " & Me.txt_dj_no & " AS Expr1, " & Me.lst_specialities_All.Column(0) & " AS Expr2;"
DoCmd.SetWarnings False
DoCmd.RunSQL strSQL
DoCmd.SetWarnings True
UnSelect_All
UnSelect_DJ
Me.lst_specialities.Requery
End Sub
Function UnSelect_All()
Dim i As Integer
For i = 0 To Me.lst_specialities_All.ListCount 'Deselect ALL rows in Listbox
lst_specialities_All.Selected(i) = False
Next i
End Function
Function UnSelect_DJ()
Dim i As Integer
For i = 0 To Me.lst_specialities.ListCount 'Deselect ALL rows in Listbox
lst_specialities.Selected(i) = False
Next i
End Function
All,
I am trying to create a search form to have users search for data using 2 parameters. I have a user form with a list box, three text boxes & a find button.
1st text box is txt_sname (Combobox)
2nd text box is txt_sdate
3rd text box is txt_sdate1
list box is lst_main
What I am trying to do here is have the user select a name from the combo box drop down & enter dates for example 12/11/2015 - 01/01/2016. Then I want the query to display in the list box.
The table I have set up which is "Main" has the following fields:
t_Name
t_Date
t_ContactID
t_Score
t_Comments
The query will look at t_Date and and t_Name, if the data matches the parameters from the text boxes it will then display the info on the list box. Can anyone point me in the right direction?
I am using the following to pass data to the table:
Private Sub c_Submit_Click()
Dim db As Database
Dim rec As Recordset
Set db = CurrentDb
Set rec = db.OpenRecordset("Select * from Main")
If IsNull(Me.txt_Name.Value) Or IsNull(Me.txt_Date.Value) Or IsNull(Me.txt_Contact.Value) Then
MsgBox ("Error! Fill out all the fields!"), vbExclamation
Exit Sub
End If
rec.AddNew
rec("t_Name") = Me.txt_Name.Value
rec("t_Date") = Me.txt_Date
rec("t_ContactID") = Me.txt_Contact
rec("t_Score") = Me.txt_Score
rec("t_Comments") = Me.txt_Comments
rec.Update
Set rec = Nothing
Set db = Nothing
Me.txt_Name = Null
Me.txt_Date = Null
Me.txt_Contact = Null
Me.txt_Score = Null
Me.txt_Comments = Null
Me.Text32.Requery
MsgBox ("Record Added Successfully!")
End Sub
Thank you everyone for the help! Cheers!
Listboxes have the RowSource property and so can use a table or query. Simply set the RowSource according to values from other textboxes:
Private Sub c_Submit_Click()
Me.lst_main.RowSource = "SELECT t_Name, t_Date, t_ContactID, t_Score, t_Comments" _
& " FROM Main" _
& " WHERE t_Name = '" & Me.txt_sname & "'" _
& " AND t_Date >= #" & Me.txt_sdate & "#" _
& " AND t_Date <= #" & Me.txt_sdate1 & "#;"
Me.lst_main.RowSourceType = "Table/Query"
Me.lst_main.Requery
End Sub