Hi
I am new to this forum and quite new to vba so if someone could help me with this code it would be appreicated.
What the code does is add's a new record to a table using the information it gets from a form and that works fine.
Below the first code is what I am trying to do.
Dim rs As ADODB.Recordset
On Error GoTo HandleError
Set rs = New ADODB.Recordset
rs.Open "tblRegister", CurrentProject.Connection, adOpenDynamic, adLockOptimistic
With rs
![IDNHHMeter] = Me.txtIDNHHMeter
![Register] = Me.txtRegister
.Update
End With
rs.Close
Set rs = Nothing
ExitHere:
Exit Sub
HandleError:
MsgBox Err.Description
Resume ExitHere
What I would like to do is put a loop on this code so I can add multiple records depending on what number is in the text box txtRegister. In each record the IDNHHMeter would stay the same but the register would start at 1 and have an increment of 1 until it gets to the number in txtRegister. Below is my code which doesnt seem to work. Any ideas?
Dim R As Integer
Dim i As Integer
Do
For i = 1 To R
Dim rs As ADODB.Recordset
On Error GoTo HandleError
Set rs = New ADODB.Recordset
rs.Open "tblRegister", CurrentProject.Connection, adOpenDynamic, adLockOptimistic
With rs
![IDNHHMeter] = Me.txtIDNHHMeter
![Register] = i
.Update
End With
rs.Close
Set rs = Nothing
Exit For
Next
Loop Until i = R
ExitHere:
Exit Sub
HandleError:
MsgBox Err.Description
Resume ExitHere
End Sub
I am new to this forum and quite new to vba so if someone could help me with this code it would be appreicated.
What the code does is add's a new record to a table using the information it gets from a form and that works fine.
Below the first code is what I am trying to do.
Dim rs As ADODB.Recordset
On Error GoTo HandleError
Set rs = New ADODB.Recordset
rs.Open "tblRegister", CurrentProject.Connection, adOpenDynamic, adLockOptimistic
With rs
![IDNHHMeter] = Me.txtIDNHHMeter
![Register] = Me.txtRegister
.Update
End With
rs.Close
Set rs = Nothing
ExitHere:
Exit Sub
HandleError:
MsgBox Err.Description
Resume ExitHere
What I would like to do is put a loop on this code so I can add multiple records depending on what number is in the text box txtRegister. In each record the IDNHHMeter would stay the same but the register would start at 1 and have an increment of 1 until it gets to the number in txtRegister. Below is my code which doesnt seem to work. Any ideas?
Dim R As Integer
Dim i As Integer
Do
For i = 1 To R
Dim rs As ADODB.Recordset
On Error GoTo HandleError
Set rs = New ADODB.Recordset
rs.Open "tblRegister", CurrentProject.Connection, adOpenDynamic, adLockOptimistic
With rs
![IDNHHMeter] = Me.txtIDNHHMeter
![Register] = i
.Update
End With
rs.Close
Set rs = Nothing
Exit For
Next
Loop Until i = R
ExitHere:
Exit Sub
HandleError:
MsgBox Err.Description
Resume ExitHere
End Sub