Cascading Combo Boxes - Dependent Box is Blank - vba

I have a table called POHeader, which shows PO Numbers and the Vendor Number for the provider, as well as other data points for each PO.
Sometimes, we exempt a specific PO from all of our delivery rules, and I created an Access form to store these exemptions. Right now, there are two combo boxes, one for PO Number and one for Vendor Number, on the form. I want the Vendor Number combo box to filter to only vendors who match the PO Number selected by the user (PO numbers may not be unique in some cases).
Here is the VBA I wrote for the AfterUpdate event on the PO Number combo box:
Private Sub PONumber_AfterUpdate()
Me.VendorNumber.RowSource = "SELECT tblPOHeader.VendorNumber " & _
"FROM tblPOHeader " & _
"WHERE tblPOHeader.PONumber = '" & Me.PONumber & "' " & _
"ORDER BY tblPOHeader.VendorNumber"
End Sub
My issue is that the Vendor Number combo box goes blank once a PO Number is selected. What did I do wrong?
Private Sub PONumber_AfterUpdate()
Me.VendorNumber.RowSource = "SELECT tblPOHeader.VendorNumber " & _
"FROM tblPOHeader " & _
"WHERE tblPOHeader.PONumber = '" & Me.PONumber & "' " & _
"ORDER BY tblPOHeader.VendorNumber"
End Sub

I solved it!
Instead of a VBA for AfterUpdate, I updated the row source for the Vendor Number field to:
enter image description here
Then just did a requery for AfterUpdate.

Related

How to query combo box of only current record/row in Access data entry form?

I have created a data entry form in Access that uses combobox for entering farmer name. The combobox is used for ease and to make sure only farmers from the list are entered. For ease combo box is re-queried as you type in.
The combobox works well for the first entry but previous farmers' names are vanished when queried for the next row. I think, Access is requerying all dropdowns rather than the current drop-down/combo-box.
The VBA for the querying drop down is given below:
Public Sub FilterComboAsYouType(combo As ComboBox, defaultSQL As String,
lookupField As String)
Dim strSQL As String
If Len(combo.Text) > 0 Then
strSQL = defaultSQL & " AND " & lookupField & " LIKE '*" & combo.Text &
"*'"
Else
strSQL = defaultSQL 'This is the default row source of combo box
End If
combo.RowSource = strSQL
combo.Dropdown
End Sub
Private Sub Combo137_Change()
FilterComboAsYouType Me.Combo137, "SELECT farmer.name,farmer.ID FROM farms INNER JOIN farmer ON
farms.ID = farmer.farm_id where farms.ID LIKE" & "'" & Form_Name & "*'", "farmer.name"
End Sub
Private Sub Combo137_GotFocus()
If Form_Name <> "" Then
FilterComboAsYouType Me.Combo137, "SELECT farmer.name,farmer.ID FROM farms INNER JOIN farmer ON
farms.ID = farmer.farm_id where farms.ID LIKE" & "'" & Form_Name & "*'", "farmer.name"
Else
FilterComboAsYouType Me.Combo137, "SELECT farmer.name,farmer.ID FROM farms INNER JOIN farmer ON
farms.ID = farmer.farm_id where farms.ID LIKE" & "'" & "NONE" & "*'", "farmer.name"
End If
End Sub
Yes, all records will show the same filtered list because there is only one combobox and property settings are reflected in all instances. Filtering a combobox RowSource based on value in another field/control is known as "cascading" or "dependent". Also, your RowSource has alias - value saved is not value displayed. When the list is filtered the display alias will not be available for records that have saved value which has been filtered out. This is a well-known issue of cascading combobox. Options for dealing with:
for any form style, only filter the list for new record or when primary value is changed, then reset to full list for existing records
for forms in Continuous or Datasheet view, include lookup table in form RecordSource, bind a textbox to descriptive field from lookup table, position textbox on top of combobox, set textbox as Locked Yes and TabStop No

filtering a query record source on two date text boxes (beginning and ending date)

I have a access form whose recordsource is a query. I want to put two date text boxes on the form and then filter the query for just those records. Can somebody show me an example how this would be done.
Add two TextBox controls to your Form and set their format to "Sort Date". Then add a Button and on the Click event add the following:
Private Sub btnFilterForm_Click()
With Me
.Filter = "[Date Field] Between #" & Format(.TextBox1.Value, "mm/dd/yyyy") & "# And #" _
& Format(.TextBox2.Value, "mm/dd/yyyy") & "#"
.FilterOn = True
End With
End Sub

Access VBA - Input Form Detect Duplicate and then Display Duplicate Record

What I Have:
I have an Access input form, the user fills in data in the form, pushes a button which triggers a VBA script which checks if the input telephone number in the [Telephone] control matches an entry in the table. If there's a duplicate it triggers a MsgBox, if not it commits the data to the table. This works and it's great. Here is the script:
Option Compare Database
Private Sub buttonNewRecord_Click()
Dim ErrorInt As Integer
Dim TeleCheck As Variant
TeleCheck = DLookup("[Telephone]", "tblLog", "[Telephone] = '" & Me.Telephone & "'")
If Not IsNull(TeleCheck) Then
MsgBox "Telephone number already exists in the database!"
ErrorInt = ErrorInt + 1
End If
If ErrorInt < 1 Then
DoCmd.GoToRecord , , acNewRec
MsgBox "Record Added!"
End If
End Sub
What I Am Trying To Do:
In the table, along with [Telephone], there is a [Date] and [Customer_Name] field. When a duplicate telephone number is detected, I would like the triggered MsgBox to display the [Date] and [Customer_Name] of the record where the duplicate was found. Like:
MsgBox "Telephone number already exists for [Customer_Name] added on [Date]!"
What I Have Tried:
Not a lot because I'm not sure what to try. Googling provides a lot of different ways to detect a duplicate but I have found no one trying to return any data from the record of the duplicate found.
Dim cstmerName as String
Dim dateAdded as String
Let cstmerName = DLookup("[Customer_Name]", "tblLog", "[Telephone] = '" & Me.Telephone & "'")
Let dateAdded = CStr(DLookup("[Date]", "tblLog", "[Telephone] = '" & Me.Telephone & "'"))
MsgBox "Telephone number already exists for " & cstmerName & " added on " & dateAdded & "!"

How to add a comment using comment boxes in Access?

I am building an Access database for work. I have set up a report to open upon click for a certain record. So only the information of that record is to appear on the report. However, I would like to add a comment box in the report where you can add comments. The new comments are stamped and added to the previous comments already showing in the report. I was able to program the commenting function in a separate report. However, for the reports that show only specific records it won't work. I know it is because I have to somehow add each comment to my database, but I just can not figure out how to do it. I used the following code that I found online in another article. It works fine when your comments are not tied to a certain record.
Private Sub cmdAppendComment_Click()
If (IsNull(txtNewComment.Value)) Then
MsgBox ("Please enter a comment before clicking" & _
"on the Append Comment button.")
Exit Sub
End If
' These commented lines will never be reached:
' If (IsNull(txtComment.Value)) Then
' Table.tblmain.User_comment.Value = txtNewComment.Value & " ~ " & _
' VBA.DateTime.Date & " ~ " & VBA.DateTime.Time
' Else
Table.tblmain.User_comment.Value = txtComment.Value & _
vbNewLine & vbNewLine & _
txtNewComment.Value & " ~ " & _
VBA.DateTime.Date & " ~ " & VBA.DateTime.Time
' End If
' txtNewComment.Value = ""
' Use Null:
txtNewComment.Value = Null
End Sub
You would use a form, not report, for this.
Bind this to the table, add the new comment to the existing comment bound to texbox, and save the record.

Run-time error: The value you entered isn't valid for this field

I am trying to update a record using Subform. When I update the first time it updates properly but when I try to update the same record again I am getting the error:
Run-time error '-2147352567 (80020009)': The value you entered isn't valid for this field
The following is the form.
When I click edit, the information from the selected record is populated into the respective text-boxes. Once I update the information and click update, the record gets successfully updated for the first time.
When I try to update the same record again I get the mentioned error.
Here is the VB script that runs on clicking edit.
Private Sub cmdEdit_Click()
'Check if data exists in the list
If Not (Me.frmschoolsub.Form.Recordset.EOF And Me.frmschoolsub.Form.Recordset.BOF) Then
'get data to text box control
With Me.frmschoolsub.Form.Recordset
Me.Schooltxt = .Fields("School_Name")
Me.Desctxt = .Fields("Description")
Me.Deantxt = .Fields("Dean")
Me.Adeantxt = .Fields("Associate_Dean")
'store id of student in tag
Me.Schooltxt.Tag = .Fields("School_ID")
'change caption of button to update
Me.cmdAdd.Caption = "Update"
Me.cmdEdit.Enabled = False
End With
End If
End Sub
When I click on Debug it highlights the following line.
Me.Schooltxt = .Fields("School_Name")
Can you help me in identifying what is the issue here.
I figured that after the each update, I am losing the position of record. I added the following statement after update and Requery
Me.frmschoolsub.Form.Recordset.MoveFirst
Following is the code snippet.
Else
CurrentDb.Execute "Update School " & _
" SET School_Name ='" & Me.Schooltxt & "'" & _
", Description ='" & Me.Desctxt & "'" & _
", Dean ='" & Me.Deantxt & "'" & _
", Associate_Dean='" & Me.Adeantxt & "'" & _
"where School_ID=" & Me.Schooltxt.Tag
End If
'Clear the Fields
cmdClr_Click
'Refresh the table
frmschoolsub.Form.Requery
Me.frmschoolsub.Form.Recordset.MoveFirst
This fixed the issue.
This error is happening that a form field cannot be referenced to a textbox like you did.
You can do it as below.
With Me.frmschoolsub.Form.Recordset
Me.Schooltxt = Forms![<<if you have main form >>]![frmschoolsub].form![School_Name]