Someone please explain this to me. I'm familiar with ADO in regards to ASP, but in Jet, I'm new.
I have a form where a user can enter a new record. The user hits the "add" button and the code uses ADO to append the record.
After this I need to refresh a listbox on the originating form so the record pops up. This has been the problem. The record will not show up no matter what I do. The listbox is on a tab control.
The record will show up if:
1. I click off of the current tab and then back, the record shows up.
2. If I move to another record using the nav buttons and then move back.
3. if I close and reopen the form.
Now here's the amazing part -
I've read on a lot of other posts that it could be a delay in ADO writing to the datbase - in essence, i guess the record doesn't show up before the the lstBox.requery method is called. Since this is a small app (split table) I didn't think that could be it - but I tried it, and lo and behold - if I put in a loop like this:
sure enough, the record shows up if I write this delay in the code.
This can't be the right way to do this. What is the right way? Here is the full code:
I am a nobody, and nobody is perfect; therefore, I am perfect.
I have a form where a user can enter a new record. The user hits the "add" button and the code uses ADO to append the record.
After this I need to refresh a listbox on the originating form so the record pops up. This has been the problem. The record will not show up no matter what I do. The listbox is on a tab control.
The record will show up if:
1. I click off of the current tab and then back, the record shows up.
2. If I move to another record using the nav buttons and then move back.
3. if I close and reopen the form.
Now here's the amazing part -
I've read on a lot of other posts that it could be a delay in ADO writing to the datbase - in essence, i guess the record doesn't show up before the the lstBox.requery method is called. Since this is a small app (split table) I didn't think that could be it - but I tried it, and lo and behold - if I put in a loop like this:
Code:
Dim n As Double
For n = 0 To 10000000
n = n + 1
Next
sure enough, the record shows up if I write this delay in the code.
This can't be the right way to do this. What is the right way? Here is the full code:
Code:
Public Sub addNewAuto()
DoCmd.SetWarnings False
Dim strSQL As String
Dim strPolID, strYear As String
Dim datEffDate As Date
Dim datExDate As Date
Dim intAutoNum As Integer
Dim strVehDescrip As String
Dim strVin As String
Dim cnString As String
Dim cnADO As New ADODB.Connection
Dim rsADO As New ADODB.Recordset
Dim frmMain As Form
Set frmMain = Forms!frmMain
'Test Code
'Set cnADO = CurrentProject.Connection
'The connection string:
cnString = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=C:\myPath\myDB.mdb;"
'Open the string:
cnADO.Open (cnString)
'Set variables:
strPolID = Me.txtPolID.Value
strYear = Nz(Me.txtYear.Value, "")
datEffDate = Nz(Me.txtEffDate.Value, Date)
datExDate = Nz(Me.txtExpDate.Value, Date)
intAutoNum = Me.txtVehicleNum.Value
strVehDescrip = Nz(Me.txtDescrip.Value, "")
strVin = Nz(Me.txtVin.Value, "")
'pull the fields from the table we want to update:
strSQL = "SELECT tblAutoSchedule.ID, tblAutoSchedule.PolicyID, tblAutoSchedule.EffDate, "
strSQL = strSQL & "tblAutoSchedule.ExDate, tblAutoSchedule.Number, tblAutoSchedule.Year, "
strSQL = strSQL & "tblAutoSchedule.VehDescrip, tblAutoSchedule.Vin "
strSQL = strSQL & "FROM tblAutoSchedule;"
rsADO.Open strSQL, cnADO, adOpenDynamic, adLockOptimistic
'rsADO.Open strSQL, cnADO
'Add the new record
rsADO.AddNew
rsADO("PolicyID") = strPolID
rsADO("EffDate") = datEffDate
rsADO("ExDate") = datExDate
rsADO("Number") = intAutoNum
rsADO("Year") = strYear
rsADO("VehDescrip") = strVehDescrip
rsADO("Vin") = strVin
'update the recordset
rsADO.Update
'rsADO.Requery
rsADO.Close
cnADO.Close
Set rsADO = Nothing
Set cnADO = Nothing
strSQL = Empty
DoCmd.SetWarnings True
'Me.Form.Recordset.Requery
'this is so incredibly stupid - this can't be right....
Dim n As Double
For n = 0 To 10000000
n = n + 1
Next
frmMain.Recordset.Requery
frmMain.lstAutoSched.Requery
End Sub
I am a nobody, and nobody is perfect; therefore, I am perfect.