尝试更新SQL记录集时Excel vba类型不匹配错误

时间:2016-03-31 16:49:10

标签: excel vba excel-vba sql-update adodb

我觉得我已经介绍了这里和其他地方发现的一些修复,但我仍然得到了#34; Type Mismatch"错误。我还应该注意到,虽然我只在这里列出了麻烦的一行,但它似乎与我的记录中的大约2/3一起使用。它不仅仅是日期字段,而是许多不同的字段。我基本上将变量更改为Variant类型,将Dates更改为Date,然后再更改为Variant。我包含了一个实际上与不起作用的字段一起工作的字段。显然,我知道这一点,因为如果我注释掉ApprovalDate字段,它就可以保存得很好。

这是我的代码:

Private Sub cmdSaveMentee_Click()

Const StrConn As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\DON\OneDrive\Documents\Connection Training Services\Database\MenteeTables.xls;Extended Properties=""Excel 8.0;HDR=Yes;"";"


        Dim MenteeConn As ADODB.Connection
        Dim rsMenteeData As ADODB.Recordset
        Dim MenteeField As ADODB.Field
        Dim varPassString As String
        Dim varSearchString
        Dim NextID As Long

        Dim MenteeID as Long
        Dim varApprovalDate as Date 'also run as Variant
        Dim varFirstName as Variant  
        Dim varLastName as Variant

            Set MenteeConn = New ADODB.Connection
            Set rsMenteeData = New ADODB.Recordset

            MenteeConn.ConnectionString = StrConn
            MenteeConn.Open

        varMenteeID = frmMenteeUpdate.txtMenteeID.Text

            varRecordCount = Application.WorksheetFunction.Subtotal(103, Worksheets("rptMenteeData").Range("C3:C150"))
            varRecordTracker = ActiveCell.Row - 2
            varSearchString = "SELECT MenteeID, ApprovalDate, FirstName, LastName FROM [tblMasterMentee$] WHERE MenteeID=" & varMenteeID & ";"
            frmMenteeUpdate.txtRecordTracker.Text = varRecordTracker & "  of  " & varRecordCount

            With rsMenteeData
                .ActiveConnection = MenteeConn
                .Source = varSearchString
                .LockType = adLockOptimistic
                .CursorType = adOpenDynamic 'I also did this with adOpenKeyset
                .Open

            End With

    varApprovalDate = "" & frmMenteeUpdate.txtApprovalDate
    varCaseManager = "" & frmMenteeUpdate.txtCaseManager
    varFirstName = "" & frmMenteeUpdate.txtFirstName

    rsMenteeData.Fields("ApprovalDate") = varApprovalDate
    rsMenteeData.Fields("CaseManager") = varCaseManager
    rsMenteeData.Fields("FirstName") = varFirstName

        rsMenteeData.Update

        CloseRecordset:
            If (Not rsMenteeData.BOF) And (Not rsMenteeData.EOF) Then
                rsMenteeData.CancelUpdate
            End If

            MsgBox "You have successfully updated " & varFirstName & " " & varLastName & "'s Electronic record"

            rsMenteeData.Close

        CloseConnection:
            MenteeConn.Close

            Set rsMenteeData = Nothing
            Set MenteeConn = Nothing

            frmMenteeUpdate.Hide

            Range("C1").Select

        End Sub

该表的更新位于单独的工作簿中。它的列是MenteeID,FirstName,LastName,ApprovalDate。 ApprovalDate是短日期格式,其他格式为常规格式。

任何想法为什么会发生这种情况将不胜感激。

0 个答案:

没有答案