Access 2010에서 ODBC를 통해 MySQL 서버에 연결된 응용 프로그램을 개발했습니다. 열VBA ADODB 거래
ID, FirstName, LastName, TelNo, MobileNo, EmailAddress, PrimaryContact, TimeStamp
및 ReportingType
:
나는 2 개 테이블 열
ContactDetails
이
ID, ReportType, ContactID, TimeStamp
가 나는 ADO 트랜잭션을 사용하고 있습니다를하지만 ContactDetails
에 삽입 할 때, 나는 필요 ID를 검색하여 ReportingType
에 해당 레코드를 삽입하고을 설정할 수 있습니다.은 ContactDetails.ID
입니다.
VB.Net에서 SQL 문 끝에 "Select LAST_INSERT_ID()"를 사용할 수 있다는 것을 알고 ExecuteScalar
은 자동으로 증가 된 ID
을 반환합니다.
다음은
Dim conn As ADODB.Connection
On Error GoTo ErrorHandler
Set conn = CurrentProject.Connection
With conn
.BeginTrans
'insert a new customer record
.Execute "INSERT INTO ContactDetails (" & _
"FirstName, " & _
"LastName , " & _
"TelNo , " & _
"MobileNo ," & _
"EmailAddress ," & _
"IsPrimaryContact) " & _
"Values (" & _
"'" & Me.FirstName & "'," & _
"'" & Me.LastName & "'," & _
"'" & Me.TeleNum & "'," & _
"'" & Me.MobileNum & "'," & _
"'" & Me.EmailAddress & "'," & _
False & ");", , adCmdText + adExecuteNoRecords
'Added from a possible solution
Dim rs As New ADODB.Recordset
Set rs = conn.Execute("SELECT @@Identity", , adCmdText)
Debug.Print rs.Fields(0).Value ' This returned 0
'Inset a new record into the ReportingType Table
For i = 1 To ListView1.ListItems.Count
If ListView1.ListItems(i).Checked Then
.Execute "INSERT INTO ReportingType " & _
"(ReportType, ContactID) " & _
"VALUES " & _
"('" & colReportType(ListView1.ListItems(i)) & "' , " & ContactID & ")"
End If
Next i
.CommitTrans
End With
ExitHere:
Set conn = Nothing
Exit Sub
ErrorHandler:
If Err.Number = -2147467259 Then
MsgBox Err.Description
Resume ExitHere
Else
MsgBox Err.Description
With conn
.RollbackTrans
'.Close
End With
Resume ExitHere
End If
End Sub
당신이 나를 도울 수 제발 내 코드?
당신은 당신이 (AN'ADODB.RecordSet'을 통해) 최신 ID 값을 반환 할 데이터를 기록 후'연락 Details'를 조회하고 옆'INSERT INTO' 문 –
가능한 복제를 사용할 수 있습니다 [마지막으로 삽입 된 행의 자동 번호 값 - MS Access/VBA] (https://stackoverflow.com/questions/1628267/autonumber-value-of-last-inserted-row-ms-access-vba) –
https : // stackoverflow.com/questions/42648/best-way-to-get-identity-of-inserted-row – braX