Colin Coleman
Registered User.
- Local time
- Today, 12:32
- Joined
- Jul 25, 2008
- Messages
- 27
I have a strange one here,
I have a routine thhat gererates SQL "on the fly" to duplicate a record (except for the PK filed) in the same table.
Code:
Function CopytoSelf(iContract As Integer) As Integer
On Error GoTo Err_CopytoSelf
Dim StrStoreSQL As String
Dim adoCon As New ADODB.Connection
Dim adoRst As New ADODB.Recordset
Dim SSql As String
Dim sFields As String
Dim iPos As String
Dim iNewRec As Integer
Set adoCon = CurrentProject.Connection
SSql = "SELECT * FROM CONTRACTS ;"
sFields = ExtractFieldNames(SSql, iContract)
iPos = InStr(sFields, ",")
If iPos = 0 Then
CopytoSelf = -1
GoTo Exit_CopytoSelf
End If
sFields = Mid(sFields, iPos + 1, Len(sFields))
StrStoreSQL = "INSERT INTO contracts (" & sFields & ") SELECT " & sFields
StrStoreSQL = StrStoreSQL & " FROM contracts where contract = " & Str(iContract) '& ";"
adoRst.Open StrStoreSQL, adoCon
iNewRec = DMax("[contract]", "contracts", "")
CopytoSelf = iNewRec
Exit_CopytoSelf:
Exit Function
Err_CopytoSelf:
Me.baba.SetFocus
Me.baba.Text = StrStoreSQL
'& vbCrLf & "Created =" & Str(iNewRec)
MsgBox Err.Description, vbExclamation, "Error " & Err.Number & " in Copytoself "
CopytoSelf = -1
GoTo Exit_CopytoSelf
End Function
This generates the following SQL:
INSERT INTO contracts ( zemp, CONTRACT_N, ztype, TITLE, FIRST_NAME, SURNAME, ADD1, ADD2, ADD3, ADD4, ADD5, STORE_SHOP, TOWN, POSITION, START_DATE, CONTINUOUS, STATEMENT_, DOB, LEAVE_DATE, RATE_OF_PA, DAYS_HRS1, DAYS_HRS2, DAYS_HRS3, DAYS_HRS4, SALARY, COMMISSION, COMMISSIO2, MONTH, SEND_ME, AREA_MANAG, LETTER_DAT, SUNDAY_RAT, NOTICE_DUE, TRIAL, ETHNIC_ORI, MARITAL_ST, CHILD_1, CHILD_2, CHILD_3, CHILD_4, CHILD_5, DISABLED, STATUS, ISLOCKED, DAILYRATE, UKONLY, SICKPAY, OTHEREMP, HOURSDATA, CARDATA, HEALTHDATA, PENSIONDATA, PAYDATA, COMMDATA, COVDATA, DUTYDATA, MOBILEDATA, SHOWSICK, SHOWOE, SHOWCAR, SHOWHEALTH, SHOWPENSION, SHOWPAY, SHOWSTAKEHOLDER, SHOWCOMM, SHOWCOV, SHOWMOBILE, ZPAYTYP) SELECT zemp, CONTRACT_N, ztype, TITLE, FIRST_NAME, SURNAME, ADD1, ADD2, ADD3, ADD4, ADD5, STORE_SHOP, TOWN, POSITION, START_DATE, CONTINUOUS, STATEMENT_, DOB, LEAVE_DATE, RATE_OF_PA, DAYS_HRS1, DAYS_HRS2, DAYS_HRS3, DAYS_HRS4, SALARY, COMMISSION, COMMISSIO2, MONTH, SEND_ME, AREA_MANAG, LETTER_DAT, SUNDAY_RAT, NOTICE_DUE, TRIAL, ETHNIC_ORI, MARITAL_ST, CHILD_1, CHILD_2, CHILD_3, CHILD_4, CHILD_5, DISABLED, STATUS, ISLOCKED, DAILYRATE, UKONLY, SICKPAY, OTHEREMP, HOURSDATA, CARDATA, HEALTHDATA, PENSIONDATA, PAYDATA, COMMDATA, COVDATA, DUTYDATA, MOBILEDATA, SHOWSICK, SHOWOE, SHOWCAR, SHOWHEALTH, SHOWPENSION, SHOWPAY, SHOWSTAKEHOLDER, SHOWCOMM, SHOWCOV, SHOWMOBILE, ZPAYTYP FROM contracts where contract = 1061
This when it hits the open recordset line crases with the error:
"SYNTAX ERROR insert into statement"
If i cut and paste this sql directly into a Access Query, it runs and asks permission to insert 1 record as this is undoable...
My questions is, Why does this fail when running from the recordset component ?
Thanks
Colin


I have a routine thhat gererates SQL "on the fly" to duplicate a record (except for the PK filed) in the same table.
Code:
Function CopytoSelf(iContract As Integer) As Integer
On Error GoTo Err_CopytoSelf
Dim StrStoreSQL As String
Dim adoCon As New ADODB.Connection
Dim adoRst As New ADODB.Recordset
Dim SSql As String
Dim sFields As String
Dim iPos As String
Dim iNewRec As Integer
Set adoCon = CurrentProject.Connection
SSql = "SELECT * FROM CONTRACTS ;"
sFields = ExtractFieldNames(SSql, iContract)
iPos = InStr(sFields, ",")
If iPos = 0 Then
CopytoSelf = -1
GoTo Exit_CopytoSelf
End If
sFields = Mid(sFields, iPos + 1, Len(sFields))
StrStoreSQL = "INSERT INTO contracts (" & sFields & ") SELECT " & sFields
StrStoreSQL = StrStoreSQL & " FROM contracts where contract = " & Str(iContract) '& ";"
adoRst.Open StrStoreSQL, adoCon
iNewRec = DMax("[contract]", "contracts", "")
CopytoSelf = iNewRec
Exit_CopytoSelf:
Exit Function
Err_CopytoSelf:
Me.baba.SetFocus
Me.baba.Text = StrStoreSQL
'& vbCrLf & "Created =" & Str(iNewRec)
MsgBox Err.Description, vbExclamation, "Error " & Err.Number & " in Copytoself "
CopytoSelf = -1
GoTo Exit_CopytoSelf
End Function
This generates the following SQL:
INSERT INTO contracts ( zemp, CONTRACT_N, ztype, TITLE, FIRST_NAME, SURNAME, ADD1, ADD2, ADD3, ADD4, ADD5, STORE_SHOP, TOWN, POSITION, START_DATE, CONTINUOUS, STATEMENT_, DOB, LEAVE_DATE, RATE_OF_PA, DAYS_HRS1, DAYS_HRS2, DAYS_HRS3, DAYS_HRS4, SALARY, COMMISSION, COMMISSIO2, MONTH, SEND_ME, AREA_MANAG, LETTER_DAT, SUNDAY_RAT, NOTICE_DUE, TRIAL, ETHNIC_ORI, MARITAL_ST, CHILD_1, CHILD_2, CHILD_3, CHILD_4, CHILD_5, DISABLED, STATUS, ISLOCKED, DAILYRATE, UKONLY, SICKPAY, OTHEREMP, HOURSDATA, CARDATA, HEALTHDATA, PENSIONDATA, PAYDATA, COMMDATA, COVDATA, DUTYDATA, MOBILEDATA, SHOWSICK, SHOWOE, SHOWCAR, SHOWHEALTH, SHOWPENSION, SHOWPAY, SHOWSTAKEHOLDER, SHOWCOMM, SHOWCOV, SHOWMOBILE, ZPAYTYP) SELECT zemp, CONTRACT_N, ztype, TITLE, FIRST_NAME, SURNAME, ADD1, ADD2, ADD3, ADD4, ADD5, STORE_SHOP, TOWN, POSITION, START_DATE, CONTINUOUS, STATEMENT_, DOB, LEAVE_DATE, RATE_OF_PA, DAYS_HRS1, DAYS_HRS2, DAYS_HRS3, DAYS_HRS4, SALARY, COMMISSION, COMMISSIO2, MONTH, SEND_ME, AREA_MANAG, LETTER_DAT, SUNDAY_RAT, NOTICE_DUE, TRIAL, ETHNIC_ORI, MARITAL_ST, CHILD_1, CHILD_2, CHILD_3, CHILD_4, CHILD_5, DISABLED, STATUS, ISLOCKED, DAILYRATE, UKONLY, SICKPAY, OTHEREMP, HOURSDATA, CARDATA, HEALTHDATA, PENSIONDATA, PAYDATA, COMMDATA, COVDATA, DUTYDATA, MOBILEDATA, SHOWSICK, SHOWOE, SHOWCAR, SHOWHEALTH, SHOWPENSION, SHOWPAY, SHOWSTAKEHOLDER, SHOWCOMM, SHOWCOV, SHOWMOBILE, ZPAYTYP FROM contracts where contract = 1061
This when it hits the open recordset line crases with the error:
"SYNTAX ERROR insert into statement"
If i cut and paste this sql directly into a Access Query, it runs and asks permission to insert 1 record as this is undoable...
My questions is, Why does this fail when running from the recordset component ?
Thanks
Colin



