简体   繁体   中英

Data Type Mismatch in criteria expression Access 2010 with SQL Insert Statement

I am trying to update a linked table in access and I am getting an error of data mismatch type in criteria expression. The only database fields that are float are the ones that start with HOURS.

I tried making the hours field an integer value, but got a different type mismatch message.

I am new with VB and access, so it could be something dumb. I appreciate any help. Here is my code.

Private Sub Command30_Click()
Dim monthFormat As String
Dim yearFormat As String
Dim fullYear As String
Dim datePerformed As String
Dim currDate As String
Dim timeEntered As String
Dim empNum As String
Dim acct As String
Dim cat As String
Dim cmnt As String
Dim firstName As String
Dim lastName As String
Dim shift As String
Dim addVacation As String
Dim hours As String

If IsNumeric(Me.Text12.Value) Then hours = CInt(Me.Text12.Value) Else: hours = Me.Text12.Value

monthFormat = Format(Me.Text10.Value, "MM")
yearFormat = Format(Me.Text10.Value, "YY")
fullYear = Format(Me.Text10.Value, "YYYY")
datePerformed = Format(Me.Text10.Value, "YYYYMMDD")
currDate = Format(DateTime.Date, "YYYYMMDD")
timeEntered = Format(DateTime.Time, "HHMMSS")
empNum = " & Me.Combo20.Column(0) & "
acct = " & Me.Combo20.Column(1) & "
cat = " & Me.Combo20.Column(3) & "
cmnt = " & Me.Combo20.Column(4) & "
firstName = " & Me.Combo20.Column(5) & "
lastName = " & Me.Combo20.Column(6) & "
shift = " & Me.Combo20.Column(7) & "
hours = " & Me.Text12.Value & "

addVacation = "insert into dbo_R_PPHRTRX" & _
"(DATE_PERFORMED, EMPLOYEE_NUMBER " & _
", JOB_NUMBER " & _
", RELEASE " & _
", ACCOUNT, ACCOUNT_CR, BATCH_ITEM, BATCH_NUMBER, BURDEN_DOLLARS, CATEGORY, DATE_TIME_BEGUN,         DATE_TIME_COMPLT, EFF_VAR_DOLLARS, EMPLOYEE_ID, EO_FLAG, FIRST_NAME " & _
", HOURS_EARNED, HOURS_WORKED, HOURS_WORKED_SET, LABOR_DOLLARS, LAST_NAME, LOCATION_CODE,     PERIOD_YYYYMM, PRODUCT_LINE, QTY_COMPLETE, RATE_VAR_DOLLARS " & _
", RELEASE_WO " & _
", SHIFT, [STATUS], [TIME], WORK_CENTER, DATE_ENTERED, DivisionID, Comment " & _
", LATE_CHARGE, OPERATION, OVERTIME, PROJECT_TASK, REFERENCE, TASK_NUMBER, TYPE_TRANSACTION,     DATACAPSERIALNUMBER, COST_ACCOUNT, CS_PERIOD, WORK_ORDER) " & _
" " & _
"  VALUES('" & datePerformed & "' , '" & empNum & "'  " & _
", switch(('" & monthFormat & "' >= 1 And '" & monthFormat & "' <= 3), ('01VH' + '" & yearFormat     & "'), ('" & monthFormat & "' >= 4 And '" & monthFormat & "' <= 6), ('02VH' + '" & yearFormat & "'),     ('" & monthFormat & "' >= 7 And '" & monthFormat & "' <= 9), ('03VH' + '" & yearFormat & "'), ('" &     monthFormat & "' >= 10 And '" & monthFormat & "' <= 12), ('04VH' + '" & yearFormat & "'))" & _
", switch(('" & monthFormat & "' >= 1 And '" & monthFormat & "' <= 3), ('01VH' + '" & yearFormat     & "'), ('" & monthFormat & "' >= 4 And '" & monthFormat & "' <= 6), ('02VH' + '" & yearFormat & "'),     ('" & monthFormat & "' >= 7 And '" & monthFormat & "' <= 9), ('03VH' + '" & yearFormat & "'), ('" &     monthFormat & "' >= 10 And '" & monthFormat & "' <= 12), ('04VH' + '" & yearFormat & "'))" & _
", '" & acct & "', '2500-X', '0', '0', '0', 'LABOR HRS', '" & datePerformed & "'  + '0600', '" &     datePerformed & "' + '0600', '0', 'ACCSS', '', '" & firstName & "' " & _
", '" & hours & "', '" & hours & "', '0', '0', '" & lastName & "', '01', '" & fullYear & "' + '"     & monthFormat & "', '01', '0', '0' " & _
", switch(('" & monthFormat & "' >= 1 And '" & monthFormat & "' <= 3), ('01VH' + '" & yearFormat     & "'), ('" & monthFormat & "' >= 4 And '" & monthFormat & "' <= 6), ('02VH' + '" & yearFormat & "'),         ('" & monthFormat & "' >= 7 And '" & monthFormat & "' <= 9), ('03VH' + '" & yearFormat & "'), ('" &                  monthFormat & "' >= 10 And '" & monthFormat & "' <= 12), ('04VH' + '" & yearFormat & "'))" & _
", '" & shift & "', '', '" & timeEntered & "', '92', '" & currDate & "', 'Jobscope', 'V' " & _
", '', '', '', '', '', '', '', '', '', '', '')"    

DoCmd.RunSQL (addVacation)


End Sub

Instead of "gluing together" a long, ugly, and potentially troublesome INSERT statement you might consider using a Recordset object to insert the new row:

Dim cdb As DAO.Database, rst As DAO.Recordset
Set cdb = CurrentDb
Set rst = cdb.OpenRecordset("SELECT * FROM dbo_R_PPHRTRX WHERE False", dbOpenDynaset)
rst.AddNew
rst!DATE_PERFORMED = datePerformed 
rst!EMPLOYEE_NUMBER = empNum 
' ... and so on for the rest of the fields
rst.Update
rst.Close
Set rst = Nothing
Set cdb = Nothing

Thanks for the assistance. It was the "glued together" strings as well as a few other things that caused the issues. I made the switches variables, so it did the calculation in the VBA query rather than the SQL query, and everything worked.

Thanks again.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM