2015-10-18 61 views
0

我原本以爲我對記錄集有問題,但現在看起來像現在做了一些修補程序,當我將一個空值傳入我的SQL查詢字符串並attaempt到EXCUTE它不能識別字符串NULL爲NULL在SQL將Excel電子表格中的記錄與數據庫中的記錄進行比較

如 rst.OPEN 「SELECT * FROM XXX WHERE Y = INT和Z = NULL」

不工作,但

rst.OPEN「SELECT * FROM XXX WHERE Y = int AND Z = datetime」

工作正常嗎?

 Sub CompareExcel() 

     ' Macro1 Macro 
     ' Macro recorded Aking 


     Dim TargetDB As String 
     Dim TargetDBUser As String 
     Dim TargetDBPassword As String 

     ThisWorkbook.Sheets("INPUT").Select 

     TargetDB = Cells(2, 14) 
     TargetDBUser = Cells(3, 14) 
     TargetDBPassword = Cells(4, 14) 

     Dim cnn As ADODB.Connection 
     Dim rst As ADODB.Recordset 
     Set cnn = New ADODB.Connection 
     Set rst = New ADODB.Recordset 
     cnn.Open TargetDB, TargetDBUser, TargetDBPassword 

      ThisWorkbook.Sheets("Summary").Select 
      ActiveWorkbook.Sheets("Summary").Range("A7:A20000").Delete 

      ThisWorkbook.Sheets("INPUT").Select 
      ActiveWorkbook.Sheets("INPUT").Select 
      RowCounter = Application.WorksheetFunction.CountA(Range("A2:A20")) 

      CountAllExcellRecords = 0 
      CountSuccessRecords = 0 
      RowInSourceSheet = 1 

       Do While RowInSourceSheet <= RowCounter 

        SourceSheet = ThisWorkbook.Sheets("INPUT").Range("B1").Offset(RowInSourceSheet, 0) 
        Worksheets(SourceSheet).Activate 
        RowCountSourceSheet = Application.WorksheetFunction.CountA(Range("A:A")) 
        CriteriaBuilder = "('" 
        ColumnCountSourceSheet = ThisWorkbook.Sheets(SourceSheet).UsedRange.Rows(1).Columns.Count 
        SourceSheetRow = 1 

         Do While SourceSheetRow < RowCountSourceSheet 

          QvAriable = " WHERE" 
          QtAble = "" 
          SourceSheetColumn = 0 

           Do While SourceSheetColumn < ColumnCountSourceSheet 


            qvAriablen = ThisWorkbook.Sheets(SourceSheet).Range("A1").Offset(0, SourceSheetColumn) 
            qvAriablem = ThisWorkbook.Sheets(SourceSheet).Range("A1").Offset(SourceSheetRow, SourceSheetColumn) 

            Worksheets(SourceSheet).Activate 
            Cells.Range("A1").Offset(SourceSheetRow, SourceSheetColumn).Activate 

             Application.Volatile 


              Select Case True 
              Case IsEmpty(ActiveCell): CellType = "Blank" 

               If qvAriablem = "" Then toSQL = " Null " 
               qvAriablem = toSQL 

               QvAriable = QvAriable & " " & qvAriablen & " = " & qvAriablem & "" 
               QtAble = QtAble & " " & ThisWorkbook.Sheets(SourceSheet).Range("A1").Offset(0, SourceSheetColumn) 

              Case Application.IsText(ActiveCell): DataType = "Text" 

               QvAriable = QvAriable & " " & qvAriablen & " = '" & qvAriablem & "'" 
               QtAble = QtAble & " " & ThisWorkbook.Sheets(SourceSheet).Range("A1").Offset(0, SourceSheetColumn) 

              Case Application.IsLogical(ActiveCell): CellType = "Logical" 

               QvAriable = QvAriable & " " & qvAriablen & " = " & qvAriablem & " " 
               QtAble = QtAble & " " & ThisWorkbook.Sheets(SourceSheet).Range("A1").Offset(0, SourceSheetColumn) 

              Case Application.IsErr(ActiveCell): CellType = "Error" 

               QvAriable = QvAriable & " " & qvAriablen & " = " & qvAriablem & " " 
               QtAble = QtAble & " " & ThisWorkbook.Sheets(SourceSheet).Range("A1").Offset(0, SourceSheetColumn) 

              Case IsDate(ActiveCell): DataType = "Date" 

               qvAriablem = Format(qvAriablem, "MM/DD/YYYY") 
               QvAriable = QvAriable & " " & qvAriablen & " = '" & Format(qvAriablem, "MM/DD/YYYY") & "'" 
               QtAble = QtAble & " " & ThisWorkbook.Sheets(SourceSheet).Range("A1").Offset(0, SourceSheetColumn) 


              Case InStr(1, ActiveCell.Text, ":") <> 0: CellType = "Time" 

               qvAriablem = Format(qvAriablem, "MM/DD/YYYY") 
               QvAriable = QvAriable & " " & qvAriablen & " = '" & Format(qvAriablem, "MM/DD/YYYY") & "'" 
               QtAble = QtAble & " " & ThisWorkbook.Sheets(SourceSheet).Range("A1").Offset(0, SourceSheetColumn) 


              Case IsNumeric(ActiveCell): CellType = "Value" 

               Worksheets("INPUT").Activate 
               RowCountExceptions = Application.WorksheetFunction.CountA(Range("I:I")) 
               Exceptions = 2 
               eXceptionCount = 0 

                Do While Exceptions <= RowCountExceptions 

                 If qvAriablen = Cells(Exceptions, 9) Then 

                 eXceptionCount = eXceptionCount + 1 
                 QvAriable = QvAriable & " " & qvAriablen & " = '" & qvAriablem & "'" 
                 QtAble = QtAble & " " & ThisWorkbook.Sheets(SourceSheet).Range("A1").Offset(0, SourceSheetColumn) 

                 End If 

                 Exceptions = Exceptions + 1 
                Loop 

                 If eXceptionCount = 0 Then 

                 QvAriable = QvAriable & " " & qvAriablen & " = " & qvAriablem & " " 
                 QtAble = QtAble & " " & ThisWorkbook.Sheets(SourceSheet).Range("A1").Offset(0, SourceSheetColumn) 


                 End If 


               End Select 


              If SourceSheetColumn < ColumnCountSourceSheet - 1 Then 
              QvAriable = QvAriable & " AND " 
              QtAble = QtAble & ", " 
              End If 

             SourceSheetColumn = SourceSheetColumn + 1 

            Loop 

           rst.Open "SELECT * FROM " & SourceSheet & QvAriable, cnn 
           'rst.Open "SELECT * FROM UNIT_FUND WHERE CURRENCY_CODE = 'GBP' AND UNIT_FUND_CODE = '150' AND FUND_NAME = 'MetLife Fidelity Cash Fund' AND EFFECTIVE_DATE = '10/21/2009' AND CEASE_DATE =" & "NULL" 
           Sheets("Summary").Range("G2").CopyFromRecordset rst 


           CountAllExcellRecords = CountAllExcellRecords + 1 

            If rst.RecordCount > 0 Then 

             CountSuccessRecords = CountSuccessRecords + 1 

            Else 

             ThisWorkbook.Sheets("Summary").Select 
             ActiveWorkbook.Sheets("Summary").Select 
             a = "SELECT * FROM " & SourceSheet & QvAriable 
             NextRow = Application.WorksheetFunction.CountA(Range("A:A")) + 1 
             Cells(NextRow, 1) = a 

            End If 

           rst.Close 

          SourceSheetRow = SourceSheetRow + 1 

         Loop 

        RowInSourceSheet = RowInSourceSheet + 1 

       Loop 

      ThisWorkbook.Sheets("Summary").Select 

      Cells(3, 3) = CountAllExcellRecords 
      Cells(4, 3) = CountSuccessRecords 
      Cells(5, 3) = CountSuccessRecords - CountAllExcellRecords 


     End Sub 
+0

是的,這是一個有點棘手與閱讀變量名稱和沒有明顯的聲明類型。你可以給我們一些你的表名的例子,因爲這在你的代碼中看起來很複雜,當然,我們看不到工作表的內容。 – Ambie

回答

0

RecordCount屬性取決於遊標的位置和類型。看來,你正在做的是檢查,看看是否有任何記錄,所以你可以用:中

If Not rst.EOF Then 

代替:

If rst.RecordCount <> 0 Then 
+0

嗨,大家好,問題是我傳遞的Null值不是返回一個記錄集,但是當我複製並粘貼查詢到SQL它對我來說運行良好。我有null作爲一個字符串也許這是問題?? – Aking88

+0

是的,這是行不通的。你不能使用'=',你需要使用'WHERE FieldName Is Null'作爲標準。 – Rory

+0

謝謝Rory現在完美工作 – Aking88

相關問題