This is an MS Access application connecting to SQL sever database through ODBC
I have a login where I need to delete records from the table before adding another records t the same table.
I get the existing records in a record set and then I loop through and delete them one by one
Next, I try adding the new records one by one from the form to the same table.
The connection times out when I try to get the table definition using CurrentDb.TableDefs.
I will post some of the code below.
I have a login where I need to delete records from the table before adding another records t the same table.
I get the existing records in a record set and then I loop through and delete them one by one
Next, I try adding the new records one by one from the form to the same table.
The connection times out when I try to get the table definition using CurrentDb.TableDefs.
I will post some of the code below.
Code:
Private Sub Command_Click()
Dim ws As DAO.Workspace
Dim rs3 As DAO.Recordset
Dim rs5 As DAO.Recordset
Dim rs3Log As DAO.Recordset
Dim rs5Log As DAO.Recordset
Dim lngID As Long
Set ws = DBEngine.Workspaces(0)
ws.BeginTrans
Set rs3 = GetRecords(Me.ID.Value)
Do Until rs3.EOF = True Or rs3.BOF = True ' loop until all records were processed
rs3.Delete
rs3.MoveNext
Loop
rs3.Close
Set rs5 = CurrentDb.TableDefs(S_TN_Table).OpenRecordset(dbOpenDynaset, dbPessimistic + dbSeeChanges)
For i = 1 To NoOfControls Step 1
ctrl1_1 = ctrlName1 & CStr(i)
ctrl1_2 = ctrlName2 & CStr(i)
Set cboctrl1_1 = sfrm.Controls(ctrl1_1)
Set cboctrl1_2 = sfrm.Controls(ctrl1_2)
If (cboctrl1_1.Value <> vbNullString) Then
With rs5
.AddNew
.Fields(s_FN_Data2).Value = Nz(Me.ID.Value, .Fields(s_FN_Data2).DefaultValue)
.Fields(s_FN_Data3).Value = Nz(cboctrl1_1.Value, Null)
.Fields(s_FN_Data4).Value = Nz(cboctrl1_2.Value, Null)
.Update
.Bookmark = .LastModified ' This repositioning allows us to look what the value the identity column was assigned.
lngID = .Fields(PK_ID).Value
End With
End If
Next I
' Attempt a commit of the transaction.
ws.CommitTrans
End Sub
Private Property Get GetRecords(Optional ByVal PK_ID As Long = 0) As DAO.Recordset
Dim rs2 As DAO.Recordset
Dim sSql As String
Dim fltr As String
fltr = " WHERE ID = " & CStr(Nz(PK_ID, 0))
sSql = "SELECT * FROM " & S_TN_Table
sSql = sSql & fltr
Set rs2 = CurrentDb.OpenRecordset(sSql, dbOpenDynaset, dbPessimistic + dbSeeChanges)
Set GetControlByRecord_ID = rs2
Set rs2 = Nothing
End Property