簡體   English   中英

數據類型不匹配條件表達式中訪問2010 SQL INSERT語句

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

我正在嘗試更新訪問中的鏈接表,並且在條件表達式中出現數據不匹配類型的錯誤。 唯一浮動的數據庫字段是以HOURS開頭的字段。

我嘗試將小時字段設為整數值,但是收到了另一種類型的不匹配消息。

我是VB和Access的新手,所以可能有些愚蠢。 感謝您的幫助。 這是我的代碼。

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

您可以考慮使用Recordset對象插入新行,而不是“粘在一起”一個冗長,丑陋且可能麻煩的INSERT語句:

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

感謝您的協助。 導致問題的原因是“粘在一起”的字符串以及其他一些原因。 我創建了switch變量,因此它在VBA查詢而不是SQL查詢中進行了計算,並且一切正常。

再次感謝。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM