Check a filtered table for specific data - ms-access

I have a form (frm_Property) that shows information for a property and within that form another form (frm_Rate) that is a filtered table that displays all rates associated with that property. I want to check the filtered rate table to make sure that there are either no rates entered or there is a current rate (ie the current date falls between the start and end date of a rate). If there is no current rate (only with other rates entered) I want to stop the close of the form and a message box to open telling the user to enter a current rate.
After searching and complying many different things, this is the code that I have currently.
Private Sub Form_Unload(Cancel As Integer)
Dim rs As DAO.Recordset
Dim currdate, check As String
Set rs = CurrentDb.OpenRecordset("SELECT * FROM tbl_Rate WHERE Property = " & Me.Property)
currdate = Date
'Check to see if the recordset actually contains rows
If Not (rs.EOF And rs.BOF) Then
rs.MoveFirst
Do Until rs.EOF = True
'Check if rate is current rate
If currdate >= [tbl_rate.Start_Date] And currdat <= [tbl_rate.End_Date] Then
check = "Good"
Exit Do
Else
'Move to the next record.
check = "Bad"
rs.MoveNext
End If
Loop
MsgBox check 'testing to see if correctly identified if current rate exists
Else
MsgBox "There are no records in the recordset."
End If
If check = "Bad" Then
MsgBox "Please enter current rate."
Cancel = True
End If
rs.Close 'Close the recordset
Set rs = Nothing 'Clean up
End Sub
Currently it can detect if no record is entered, but it always tells me that there is no current rate even if there is and I am not sure why.
I also have this code located within frm_rate. I don't know if that is why I am having difficulty stopping the form from closing. I was reading about on unload stopping the form from closing which is why I chose that one. I suspect I am having trouble because my "On Unload" is in the subform.
Any insight would be much appreciated.

Unload event for subform occurs after Unload of main form, so the code in subform is useless. In main form you need to save the data first Me.Dirty = false After this your code will be able to see new data in table. Also you can use RecordsetClone property of subform for accessing the data in in subform.

Use the RecordsetClone of the subform and FindFirst:
Dim currdate As String
Set rs = Me!NameOfYourSubformCONTROL.Form.RecordsetClone
' Check to see if the recordset actually contains rows.
If rs.RecordCount = 0 Then
check = "Empty"
Else
' Check if rate is current rate.
currdate = Format(Date, "yyyy\/mm\/dd"
rs.FindFirst "#" & currdate "# Between tbl_rate.Start_Date And tbl_rate.End_Date"
If Not rs.NoMatch Then
check = "Good"
Else
check = "Bad"
End If
End If

Related

MS Access Recordset Moving on Search even if no match found?

This is driving me nuts. I have an access VBA form where I want to allow users to type a document number into a box in order to jump directly to that record.
The underlying table as a field "DocNum", and the number is always sequential. I want the searchbox to display the current Doc Number the user is on, if they type in a different number and hit enter, it should either jump to that record if it exists, or if it doesn't exist, stay on the current record.
I'm trying to accomplish thisby having a hidden textbox bound to "DocNum", and an unbound visible box on top of it. On Form_Current() the unbound box is made to match the underlying field. After, update I run the following code to perform the search.
Private Sub txt_DocNumSearch_AfterUpdate()
Dim rs As Object
Set rs = Me.RecordsetClone
With rs
rs.FindFirst "[DocNum] = " & Str(Me![txt_DocNumSearch])
If rs.NoMatch Then
MsgBox "Record not found."
GoTo Cleanup
Else
Me.Bookmark = rs.Bookmark
Exit Sub
End If
End With
Cleanup:
rs.Close
Set rs = Nothing
Set dbs = Nothing
End Sub
What's driving me insane is that even when it doesn't find a match.... it skips to the next record anyway. No matter what I do... move last, trying to bookmark before I even search, etc... I can't get it to not jump forward. What am I missing?
Try simplifying it:
Private Sub txt_DocNumSearch_AfterUpdate()
Dim rs As DAO.Recordset
Set rs = Me.RecordsetClone
With rs
.FindFirst "[DocNum] = " & Str(Me![txt_DocNumSearch])
If .NoMatch Then
MsgBox "Record not found."
Else
Me.Bookmark = .Bookmark
End If
.Close
End With
End Sub

Access 2013, Extract number from field to use in search

Have a sales proposal access database for work that has a field that you can put a earlier corresponding proposal number as a reference. If you click on a button under that field it will take you directly to that earlier record. There are times we have a prefix in front of the number A-12345, E-12345 or it might just be 12345.
I need to be able to grab just the number without the letter and - for the search to work correctly. Thanks
Here is the image of my screen
Assuming you have a table with columns Proposal and Reference and a single form with controls txtReference and txtProposal, put this code to the On_Click event of your form button (I'm using DAO):
Dim strProposal As String
Dim i As Integer
Dim rs As DAO.Recordset
If Len(Nz(Me.txtReference, "")) < 1 Then
MsgBox "No reference number entered"
Else
For i = 1 To Len(Me.txtReference)
If IsNumeric(Mid(Me.txtReference, i, 1)) Then
strProposal = strProposal & Mid(Me.txtReference, i, 1)
End If
Next
End If
Set rs = Me.RecordsetClone
rs.MoveFirst
rs.FindFirst "Proposal = '" & StrProposal & "'"
If rs.NoMatch Then
MsgBox "Original proposal not found"
Else
Me.Bookmark = rs.Bookmark
Me.txtProposal.SetFocus
End If
rs.Close
Set rs = Nothing

VBA code to check the status field of all records

I just ran into a problem with my one part of my code. I have a command button that when it is pressed it determines what day of the week it is. On a certain day it is supposed to check the status field and for each record that is labeled "Needs Ordered" a query is run and is opened. This works fine unless the last record that was opened is marked different (OK or Ordered). I need it to run no matter how the last record opened was labeled.
VBA Code to change font and size in an email from access
If Weekday(Now()) = vbSunday Then
If Forms![Admin Box List].Status.Value = "Needs Ordered" Then
DoCmd.OpenForm "OrderForm"
End If
End If
Then just leave out that condition:
Dim rs As DAO.Recordset
If Weekday(Date) = vbSunday Then
Set rs = Me.RecordsetClone
rs.FindFirst "Status = '" & "Needs Ordered" & "'"
If rs.NoMatch = False Then
DoCmd.OpenForm "OrderForm"
End If
End If
Set rs = Nothing

Microsoft Access Sub Form Write Conflict Troubles

I have a form which contains a subform which displays editable fields linked to one my tables. For a project I'm currently working on, one of the requirements is that I have to track when the last change was made to a record and who did so.
So what I've done is for each editable textbox or combobox within the form and subform I've made it so they have events on their BeforeUpdate and AfterUpdate events.
For example my BeforeUpdate for a textbox:
Private Sub textbox_BeforeUpdate(Cancel As Integer)
If Not isValidUser Then
Cancel = True
Me.textbox.Undo
End If
End Sub
and my AfterUpdate is:
Private Sub textbox_AfterUpdate()
updateRecord Me.textbox.Value, UserNameWindows
End Sub
and updateRecord is:
Public Sub updateRecord(bucNumber As String, updater As String)
Dim Dbs As Object
Dim rst As Object
Dim fldEnumerator As Object
Dim fldColumns As Object
sqlStatement = "SELECT fName " & _
"FROM t_Staff " & _
"WHERE uName='" & updater & "';"
'Getting fullname of user via username
Set rst = CurrentDb.OpenRecordset(sqlStatement)
'Setting fullname to updater variable
updater = rst(0)
'Clean Up
Set rst = Nothing
'Opening Bucket Contents
Set Dbs = CurrentDb
Set rst = Dbs.OpenRecordset("Bucket Contents")
Set fldColumns = rst.Fields
'Scan the records from beginning to each
While Not rst.EOF
'Check the current column
For Each fldEnumerator In rst.Fields
'If the column is named Bucket No
If fldEnumerator.Name = "Bucket No" Then
'If the Bucket No of the current record is the same as bucketNumber
If fldEnumerator.Value = bucNumber Then
'Then change the updated fields by updater and todays date
rst.Edit
rst("Last Updated By").Value = updater
rst("Last Updated On").Value = Date
rst.Update
End If
End If
Next
'Move to the next record and continue the same approach
rst.MoveNext
Wend
'Clean Up
Set rst = Nothing
Set Dbs = Nothing
End Sub
Okay now is the weird thing, this works totally fine when I make a modification to a control within the Main form, however as soon as a try to alter something in the subform it throws up a write conflict.
If I opt to save record it ignores my code for updating who last modified it and when and if I opt to discard the change it runs my code and updates it that it has been changed!
Anyone know what is wrong or of a better way to do this?

Cannot Update. Database or Object is Read-only after Requery

Okay, cannot find anything online about this error, so here goes.
Using Access 2003. I have a form that has a combobox dropdown. Users choose from the dropdown, and the VBA code looks at a particular table to see if data already exists. If it finds it, it displays the data associated with the record. If it does not find it, it adds a new record to the table, then tries to display the new data. The problem is that when using the following code, it does not display the new data, unless I add in Me.Requery. However, if I do that, it then gives me the above error if I try to do the same process again.
So, how can I get the data to display after adding without the above error?
Here is the relevant code...
' Find the record that matches the control.
Dim rs As DAO.Recordset
Set rs = Me.Recordset.Clone
'if no record found, then add a default record
If IsNull(Combo119) Then
Set rs = CurrentDb.OpenRecordset("SELECT * FROM [Master Reject Data]")
rs.AddNew
rs.Fields("production Date") = Date
rs.Fields("Work Order #") = Combo119.Column(0)
rs.Fields("Product Type") = ""
rs.Fields("Shift") = ""
rs.Fields("Line") = ""
rs.Fields("# Produced") = 0
rs.Fields("Reject Type") = ""
rs.Fields("Reject Quantity") = 0
rs.Fields("Reject Category") = "Rejection"
Dim tRec As Long
tRec = Combo142.ItemData(0)
rs.Fields("Report Number") = tRec
rs.Update
Me.Requery 'this is the line I added to try to get the data to appear
Set rs = Me.Recordset.Clone
rs.FindFirst "[Report Number] = " & tRec 'navigate to the newly added record
Else
rs.FindFirst "[Report Number] = " & Nz(Me![Combo119], 0) 'navigate to the record with the requested report number
End If
If Not rs.EOF Then Me.Bookmark = rs.Bookmark
Sorry, found the issue after looking at it again. The problem is that the Me.Requery statement needs to be Me.Recordset.Requery. After doing that and adding Me.Refresh, it works as intended.
To avoid requerying I think you should open the recordset specifying that it should be a dynaset
CurrentDb.OpenRecordset("SELECT * FROM [Master Reject Data]", dbOpenDynaset) - in case you want to make it a bit neater. as for the small bug, I see you've already found yourself