条件表达式中的数据类型不匹配使用SQL插入语句访问2010

时间:2014-06-20 03:33:11

标签: sql vba ms-access access-vba

我正在尝试更新访问中的链接表,我在条件表达式中收到数据不匹配类型的错误。浮动的唯一数据库字段是以HOURS开头的那些。

我尝试将hours字段设为整数值,但得到了不同的类型不匹配消息。

我是VB和访问的新手,所以它可能是愚蠢的。我感谢任何帮助。这是我的代码。

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

2 个答案:

答案 0 :(得分:2)

而不是&#34;粘在一起&#34;一个冗长,丑陋且可能很麻烦的INSERT语句,您可以考虑使用Recordset对象插入新行:

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

答案 1 :(得分:0)

感谢您的帮助。它是“粘在一起”的字符串以及引起问题的其他一些东西。我做了开关变量,所以它在VBA查询而不是SQL查询中进行了计算,一切正常。

再次感谢。