Set second nested subform check box to true in Access VBA - vba

I have a form that has Work Orders, PK=OrderID. A subform that has the OrderDetails, PK=OrderDetailsID. And a subform on that subform that has OrderDetailAccessories, PK=OrderAccID.
When I set a check box on the main form to True, I want all the check boxes for the subform OrderDetails to change to True and set the CompDate to todays date AND all the check boxes for it's subform OrderDetailsAccessories to change to True and set the CompDate to today's date.
In my code, the recordset rs returns the records expected but I get an empty recordset for rs2. I stepped through the code and orddetid returns the correct value.
Private Sub IsComplete_AfterUpdate()
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim rs2 As DAO.Recordset
Dim strItemComp As String
Dim strAccComp As String
Dim ordid As Long
Dim orddetid As Long
ordid = Me.txtOrdID
strItemComp = "SELECT OrderDetailID, IsComplete, CompDate FROM tblOrderDetails WHERE OrderID = " & ordid
strAccComp = "SELECT OrderAccID, IsComplete, CompDate FROM tblOrderAcc WHERE OrderDetailID = " & orddetid
Set db = CurrentDb
Set rs = db.OpenRecordset(strItemComp)
If Me.IsComplete = True Then
If MsgBox("Marking main order complete will mark ALL items and accessories for this Order as complete!", vbYesNo, "Are you sure?") = vbYes Then
Me!txtCompletionDate = Date
rs.MoveFirst
Do Until rs.EOF
If rs!IsComplete = False Then
rs.Edit
rs!IsComplete = True
rs!CompDate = Date
rs.Update
End If
orddetid = rs.Fields("OrderDetailID").Value
Debug.Print orddetid
Set rs2 = db.OpenRecordset(strAccComp)
If rs2.RecordCount > 0 Then
rs2.MoveFirst
Do Until rs2.EOF
If rs2!IsComplete = False Then
rs2.Edit
rs2!IsComplete = True
rs2!CompDate = Date
rs2.Update
End If
rs2.MoveNext
Loop
End If
rs.MoveNext
Loop
Me.Dirty = False
Exit Sub
Else
Me.Undo
End If
Else
Me.txtCompletionDate = Null
Exit Sub
End If
Me.Dirty = False
End Sub

You must update strAccComp for each record, something like:
If Me.IsComplete = True Then
If MsgBox("Marking main order complete will mark ALL items and accessories for this Order as complete!", vbYesNo, "Are you sure?") = vbYes Then
Me!txtCompletionDate = Date
Me.Dirty = False
Set rs = Me!NameOfSubformControl.Form.RecordsetClone
rs.MoveFirst
Do Until rs.EOF
If rs!IsComplete = False Then
rs.Edit
rs!IsComplete = True
rs!CompDate = Date
rs.Update
End If
strAccComp = "SELECT OrderAccID, IsComplete, CompDate FROM tblOrderAcc WHERE OrderDetailID = " & rs!orddetid.Value
Set rs2 = CurrentDb.OpenRecordset(strAccComp)
If rs2.RecordCount > 0 Then
rs2.MoveFirst
Do Until rs2.EOF
If rs2!IsComplete = False Then
rs2.Edit
rs2!IsComplete = True
rs2!CompDate = Date
rs2.Update
End If
rs2.MoveNext
Loop
End If
rs2.Close
rs.MoveNext
Loop
rs.Close
End If
End If

Related

Deleting from recordset

I have a sub that deletes the records in recordset2 based on the records of recordset1.
The function works but very slow. Recordset1 has 300 records, Recordset2 73000 records.
Is there any way to speed this up?
Is it possible to use a filter, or a refiltered recordset?
Public Sub Erase()
DoCmd.SetWarnings False
Dim db As DAO.Database
Dim rs1 As DAO.Recordset
Dim rs2 As DAO.Recordset
Set db = CurrentDb()
Set rs1 = db.OpenRecordset("Tbl_1", dbOpenTable)2
Set rs2 = db.OpenRecordset("KISS_2", dbOpenDynaset)2
If rs1.RecordCount > 0 Then
rs1.MoveLast
rs1.MoveFirst
lngCountRecordsRs1 = rs1.RecordCount
Do Until rs1.EOF
rs2.MoveFirst
Do Until rs2.EOF
If rs1!ID = rs2!ID Then
With rs2
.Delete
End With
End If
rs2.MoveNext
Loop
rs1.MoveNext
Loop
End If
rs2.Close
rs1.Close
Set rs2 = Nothing
Set rs1 = Nothing
Set db = Nothing
Errorhandler:
End Sub
Eventually I solved the problem with .FindFirst.
Thanks for the input!
With rs2
.FindFirst "Id = " & rs1![ID]
If rs2.NoMatch Then
Else
.Delete
Exit Do
End If
End With

MS Access VBA to update a Combobox table field

I have a table with a field with the display control set to Combo Box and I have not been able to read or write to it using an OpenRecordSet. What would I have to do to modify to get these scenarios to work?
Sub TryToRead()
Set db = CurrentDb
Set rs = db.OpenRecordset("tbl", dbOpenDynaset)
x = rs!FieldName '<------Combo Box Field. x shows no info.
End Sub
Sub TryToWrite()
Set db = CurrentDb
Set rs = db.OpenRecordset("tbl", dbOpenDynaset)
With rs
.AddNew
rs!FieldName = "Test Value" '<------ Results in Run-time error 64224 Application-defined or object-defined error
.Update
End With
End Sub
I think I have what I need.
Sub Testing()
Set db = CurrentDb
Set rs = db.OpenRecordset("tbl", dbOpenDynaset)
MyStr = Array("Value1", "Value2")
Do Until rs.EOF = True
Set rs2 = rs!FieldName!Value
rs.Edit
For Each c In MyStr
rs2.AddNew
rs2!Value.Value = c
rs2.Update
Next c
rs.Update
rs.MoveNext
Loop
End Sub

Access VBA not working when already executed previously

I'm pretty new at VBA. I have a form with a text box and a check box. If I open the form and click the check box without populating the text box, I get a message box to enter comments.
However, in the same session, if I unchecked the box and recheck it, I do not get the message box to enter comments even tho the text box is blank.
How can I get the code to "refire" in the same session to give me the message box to enter comments?
Below is my code:
Private Sub Check29_Click()
If IsNull(Me.Text16) Then
MsgBox "Comments are Required.", vbCritical
Me.Check29 = Null
Exit Sub
Else
If Me.Check29 = -1 Then
Dim RS As DAO.Recordset
Set RS = CurrentDb.OpenRecordset("Exclusions", dbOpenDynaset)
RS.AddNew
RS("HW535ID") = Me![HWID]
RS("Excluded") = "Yes"
RS("BOA Assignee") = Me![AssignedBA]
RS("Comments") = Me![Text16]
RS("CheckBox") = Me![Check29]
RS("Date of Exclusion") = Me![Text115]
RS("ReviewID") = Me![Text33]
RS.Update
RS.Close
Set RS = Nothing
Exit Sub
Else
DoCmd.SetWarnings False
DoCmd.OpenQuery ("RemoveExclusion")
Me.Text16 = Null
Exit Sub
End If
End If
End Sub
I can see a logic problem in the code. Please be aware that this line:
Me.Check29 = Null
will re-trigger the _click event and produce unexpected results. And it is also incorrect (should be Me.Check29.Value=False). Please try the revised version below:
Declare a module level variable
Option Explicit
Private bCancel as Boolean
The event code (I also made more corrections):
Private Sub Check29_Click()
if bCancel Then Exit Sub
bCancel = False
If Trim(Me.Text16.Text) = vbnullstring Then
MsgBox "Comments are Required.", vbCritical
bCancel = True
Me.Check29.Value = False
bCancel = False
Exit Sub
Else
If Me.Check29.Value = True Then
Dim RS As DAO.Recordset
Set RS = CurrentDb.OpenRecordset("Exclusions", dbOpenDynaset)
RS.AddNew
RS("HW535ID") = Me![HWID]
RS("Excluded") = "Yes"
RS("BOA Assignee") = Me![AssignedBA]
RS("Comments") = Me![Text16]
RS("CheckBox") = Me![Check29]
RS("Date of Exclusion") = Me![Text115]
RS("ReviewID") = Me![Text33]
RS.Update
RS.Close
Set RS = Nothing
Exit Sub
Else
DoCmd.SetWarnings False
DoCmd.OpenQuery ("RemoveExclusion")
Me.Text16.Text = vbnullstring
Exit Sub
End If
End If
End Sub
Private Sub Check29_Click()
If bCancel Then Exit Sub
bCancel = False
Me.Text16.SetFocus
If Trim(Me.Text16.Text) = vbNullString And Me.Check29.Value = True Then
MsgBox "Comments are Required.", vbCritical
bCancel = True
Me.Check29.Value = False
bCancel = False
Exit Sub
Else
If Me.Check29.Value = True Then
Dim RS As DAO.Recordset
Set RS = CurrentDb.OpenRecordset("Exclusions", dbOpenDynaset)
RS.AddNew
RS("HW535ID") = Me![HWID]
RS("Excluded") = "Yes"
RS("BOA Assignee") = Me![AssignedBA]
RS("Comments") = Me![Text16]
RS("CheckBox") = Me![Check29]
RS("Date of Exclusion") = Me![Text115]
RS("ReviewID") = Me![Text33]
RS.Update
RS.Close
Set RS = Nothing
Exit Sub
Else
If Me.Check29.Value = False Then
DoCmd.SetWarnings False
DoCmd.OpenQuery ("RemoveExclusion")
Me.Text16 = ""
Exit Sub
End If
End If
End If
End Sub

The checking is always true after first loop in VBA in Access

The "Check" somehow is always 0 after first loop, I keep debugging but still cannot find out why. Any idea? The data suppose to make "check" be 0 sometimes but not all the time.
Private Sub Command12_Click()
Dim db As Database
Dim rs As DAO.Recordset
Dim rs2 As DAO.Recordset
Set db = CurrentDb()
Set rs = db.OpenRecordset("Amity")
Set rs2 = db.OpenRecordset("Opportunity")
Set rs3 = db.OpenRecordset("SalesForceDonor")
Set rs4 = db.OpenRecordset("Donor")
While Not rs.EOF
check = 0
While Not rs3.EOF
If rs("Donor_Code") = rs3("Donor_Code") Then
check = 1
End If
rs3.MoveNext
Wend
If check = 0 Then
rs4.AddNew
rs4![Donor_Code] = rs![Donor_Code]
rs4.Update
End If
rs2.AddNew
rs2![Donor_Code] = rs![Donor_Code]
rs2![Donation_name] = rs![Donation_name]
rs2.Update
rs.MoveNext
Wend
rs3.Close
rs4.Close
rs2.Close
rs.Close
End Sub
I've found somethig that must be corrected, adding rs3.MoveFirst for each record of rs:
Private Sub Command12_Click()
Dim check
Dim db As Database
Dim rs As DAO.Recordset
Dim rs2 As DAO.Recordset
Set db = CurrentDb()
Set rs = db.OpenRecordset("Amity")
Set rs2 = db.OpenRecordset("Opportunity")
Set rs3 = db.OpenRecordset("SalesForceDonor")
Set rs4 = db.OpenRecordset("Donor")
While Not rs.EOF
check = 0
rs3.MoveFirst ' <= here we move to the first record of rs3!!!
Do While Not rs3.EOF
If rs("Donor_Code") = rs3("Donor_Code") Then
check = 1
Exit Do
End If
rs3.MoveNext
Loop
If check = 0 Then
rs4.AddNew
rs4![Donor_Code] = rs![Donor_Code]
rs4.Update
End If
rs2.AddNew
rs2![Donor_Code] = rs![Donor_Code]
rs2![Donation_name] = rs![Donation_name]
rs2.Update
rs.MoveNext
Wend
rs3.Close
rs4.Close
rs2.Close
rs.Close
End Sub

The remote server machine does not exist or is unavaliable (error # 462)

So I have this code in Mcirosoft Outlook. The code runs when new mail comes in, and depending on the sender's name and the attachments, it saves the text files and imports the data into 2 access databases and and runs certain queries pre built in the database. The code errors out when 2 emails which are from the right sender and has the right attachments comes in. The code processes the first email correctly however when the second email is getting processed, the code errors out at the bolded line below.
Option Explicit
Private Sub Application_NewMail()
Dim ns As NameSpace
Dim inbox As MAPIFolder
Dim Item As MailItem
Dim atmt As Attachment
Dim fso As FileSystemObject
Dim fs As TextStream
Dim dt, invfn, misfn, invdr, misdr, dbfn As String
Dim invt, mist As Boolean
Dim db As Object
Set ns = GetNamespace("MAPI")
Set inbox = ns.GetDefaultFolder(olFolderInbox)
Set fso = New FileSystemObject
If inbox.UnReadItemCount = 0 Then
Exit Sub
Else
For Each Item In inbox.Items.Restrict("[UnRead] = True")
If Item.SenderName = "Menon, Jayesh" Then
dt = Left(Right(Item.Subject, 12), 10)
For Each atmt In Item.Attachments
If atmt.FileName = "InvalidLoans.txt" Then
invfn = "ERLMF_InvalidLoans_" & dt & ".txt"
invdr = "C:\Documents and Settings\U299482\Desktop\Data Drop\" & _
invfn
atmt.SaveAsFile invdr
Set fs = fso.OpenTextFile(invdr)
If fs.Read(23) = "Invalid Loans Count = 0" Then
invt = False
Else
invt = True
End If
fs.Close
End If
If atmt.FileName = "MissingLoans.txt" Then
misfn = "ERLMF_MissingLoans_" & dt & ".txt"
misdr = "C:\Documents and Settings\U299482\Desktop\Data Drop\" & _
misfn
atmt.SaveAsFile misdr
Set fs = fso.OpenTextFile(misdr)
If fs.Read(23) = "Missing Loans Count = 0" Then
mist = False
Else
mist = True
End If
fs.Close
End If
Next
If invt = True Or mist = True Then
Set db = CreateObject("Access.Application")
dbfn = "C:\Documents and Settings\U299482\Desktop\Databases\BPDashboard.accdb"
With db
.OpenCurrentDatabase dbfn, True
.Visible = True
If invt = True Then
.DoCmd.TransferText acImportDelim, "Lns_Spec", "Invalid_Lns", invdr, True
End If
If mist = True Then
.DoCmd.TransferText acImportDelim, "Lns_Spec", "Missing_Lns", misdr, True
End If
.Quit
End With
Set db = Nothing
End If
If invt = True Then
Set db = CreateObject("Access.Application")
dbfn = "C:\Documents and Settings\U299482\Desktop\Databases\CORE IDP.accdb"
With db
.OpenCurrentDatabase dbfn, True
.Visible = True
**CurrentDb.Execute "A0_Empty_ERLMF_InvalidLoans_2013-04-02", dbFailOnError**
.DoCmd.TransferText acImportDelim, "Lns_Spec", "ERLMF_InvalidLoans_2013-04-02", invdr, True
CurrentDb.Execute "AppendERLMF", dbFailOnError
CurrentDb.Execute "FaxRF Crystal Append", dbFailOnError
.Quit
End With
Set db = Nothing
End If
Item.UnRead = False
End If
Next
End If
End Sub
I think you're getting over-lapping .Execute commands. You need to ensure that the first execution finishes before starting the next. To fix, I'd start by declaring a Public variable Executing then move the below code into its own method.
Sub Execute()
Executing = True
Set db = CreateObject("Access.Application")
dbfn = "C:\Documents and Settings\U299482\Desktop\Databases\CORE IDP.accdb"
With db
.OpenCurrentDatabase dbfn, True
.Visible = True
CurrentDb.Execute "A0_Empty_ERLMF_InvalidLoans_2013-04-02", dbFailOnError
.DoCmd.TransferText acImportDelim, "Lns_Spec", "ERLMF_InvalidLoans_2013-04-02", invdr, True
CurrentDb.Execute "AppendERLMF", dbFailOnError
CurrentDb.Execute "FaxRF Crystal Append", dbFailOnError
.Quit
End With
Set db = Nothing
Executing = False
End Sub
Then, when calling the function, surround it with a loop that tests to see if Executing is false.
Do
If Executing = False Then
Execute
Exit Do
End If
Loop