sathyaganapathi
Board Regular
- Joined
- Apr 29, 2021
- Messages
- 81
- Office Version
- 2016
- Platform
- Windows
Hello,
I have a excel vba code used with userform to enter data into access database.
The code worke fine for single entry. But, some times I have to enter the same record multiple times, say 5 to 6 times. I tried with 'for loop'. but it is thowing error saying "operation is not allowed when the object is open".
Could someone help me on this please?
Code is as below which works fine for single entry.
Code used with for loop to copy same record upto 6 times.
I have a excel vba code used with userform to enter data into access database.
The code worke fine for single entry. But, some times I have to enter the same record multiple times, say 5 to 6 times. I tried with 'for loop'. but it is thowing error saying "operation is not allowed when the object is open".
Could someone help me on this please?
Code is as below which works fine for single entry.
VBA Code:
Private Sub Save1_Click()
Dim cnn As New ADODB.Connection
Dim rst As New ADODB.Recordset
Dim qry As String
cnn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & ThisWorkbook.Path & "\PlabInputDb.accdb"
If Me.txtId.Value <> "" Then
qry = "SELECT * FROM TBL_PlabInput WHERE ID = " & Me.txtId.Value
Else
qry = "SELECT * FROM TBL_PlabInput Where ID = 0"
End If
rst.Open qry, cnn, adOpenKeyset, adLockOptimistic
If rst.RecordCount = 0 Then
rst.AddNew
End If
rst.Fields("Process_DateTime").Value = CDate(Me.txtDate.Value)
rst.Fields("File_Name").Value = Me.txtFileName.Value
rst.Fields("Order_No").Value = Me.OrdNo.Value
rst.Update
Me.txtId.Value = ""
Me.txtDate.Value = VBA.Format(Now(), "mm/dd/yyyy HH:mm")
Me.txtFileName.Value = ""
Me.OrdNo.Value = ""
rst.Close
cnn.Close
MsgBox "Updated Successfully", vbInformation
Call Me.List_box_Data
End Sub
Code used with for loop to copy same record upto 6 times.
VBA Code:
Private Sub Save1_Click()
Dim cnn As New ADODB.Connection
Dim rst As New ADODB.Recordset
Dim qry As String
cnn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & ThisWorkbook.Path & "\PlabInputDb.accdb"
On Error GoTo Get_Out
RunTimes = InputBox("enter the number of line to copy the Sample data?", "Copy Sample (max 6 lines)")
On Error GoTo 0
If RunTimes > 6 Then
MsgBox "enter value 6 or less only"
Exit Sub
ElseIf RunTimes = 0 Then
Exit Sub
Else
For X = 1 To RunTimes
If Me.txtId.Value <> "" Then
qry = "SELECT * FROM TBL_PlabInput WHERE ID = " & Me.txtId.Value
Else
qry = "SELECT * FROM TBL_PlabInput Where ID = 0"
End If
rst.Open qry, cnn, adOpenKeyset, adLockOptimistic
If rst.RecordCount = 0 Then
rst.AddNew
End If
rst.Fields("Process_DateTime").Value = CDate(Me.txtDate.Value)
rst.Fields("File_Name").Value = Me.txtFileName.Value
rst.Fields("Order_No").Value = Me.OrdNo.Value
rst.Update
Next X
Get_Out:
End If
Me.txtId.Value = ""
Me.txtDate.Value = VBA.Format(Now(), "mm/dd/yyyy HH:mm")
Me.txtFileName.Value = ""
Me.OrdNo.Value = ""
rst.Close
cnn.Close
MsgBox "Updated Successfully", vbInformation
Call Me.List_box_Data
End Sub