简体   繁体   English

数据类型不匹配条件表达式中访问2010 SQL INSERT语句

[英]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. 唯一浮动的数据库字段是以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. 我是VB和Access的新手,所以可能有些愚蠢。 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: 您可以考虑使用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

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. 我创建了switch变量,因此它在VBA查询而不是SQL查询中进行了计算,并且一切正常。

Thanks again. 再次感谢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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