Access 2000中的VB:ado查询每次都得到不同的结果?

时间:2011-03-18 04:18:26

标签: ms-access vba ado

我得到了一个表单,其中有2个按钮,1个是设置输入数据文件,1个是启动子,sub应该对db进行查询。但同样简单的查询“select * from opt_in_customer_record;”回归不同的东西!那太糟了!为什么???

这是我的代码,btnBrowse_Click()将弹出窗口供用户选择文件,每次我都会使用相同的文件。 btnGenData_Click()是遇到问题的子。

对于数据文件,这里是前20行,Event_Plan_Code是第一列。 5BUDP;香港; 050111; 520010100000800 5BUDP;香港; 010111; 520010100100867 5BUDP;香港; 130111; 520010100182001 3BUDP;香港; 050111; 520010100244746 5BUDP;香港; 040111; 520010100282676 1BUDP;香港; 110111; 520010100310573 1BUDP;香港; 120111; 520010100310573 3BUDP;香港; 310111; 520010100361924 1BUDP;香港; 310111; 520010100392644 1BUDP;香港; 290111; 520010100406914 3BUDP;香港; 280111; 520010100429143 3BUDP;香港; 190111; 520010100440403 3BUDP;香港; 300111; 520010100482444 1BUDP;香港; 130111; 520010100523409 3BUDP;香港; 210111; 520010100576847 5BUDP;香港; 230111; 520010100583232 3BUDP;香港; 200111; 520010100637103 3BUDP;香港; 160111; 520010100639083 3BUDP;香港; 190111; 520010100666157 3BUDP;香港; 250111; 520010100774408

如果Event_Plan_Code的第一个字符是1,我让程序停止,只是为了停止程序进行调试。每次按下按钮,我得到的结果都不同:

第一轮: 5BUDP 5BUDP 5BUDP 3BUDP 5BUDP 1BUDP

这是有道理的。

第二轮: 3BUDP 1BUDP

问题是查询应该重新开始,结果应该是一样的!现在我得到了不同的结果。

非常感谢您回答我的问题!

Option Compare Database
Private Sub btnBrowse_Click()

Dim filePath As String

filePath = LaunchCD(Me)

txtFilePath.Value = filePath
txtStatus.Value = ""
End Sub

Private Sub btnGenData_Click()
'On Error GoTo Error_Handling

Dim extractCdrFlag As Boolean


txtStatus.Value = ""
If IsNull(txtFilePath.Value) Then
    MsgBox "Please enter a valid input file location."
Else
    txtStatus.Value = ""
    txtStatus.Value = txtStatus.Value & "Deleting previous record from table Opt_In_Customer_Record..." & vbCrLf
    CurrentDb.Execute "deleteAll"
    txtStatus.Value = txtStatus.Value & "Delete successfully." & vbCrLf
    If FileExists(txtFilePath.Value) Then
            txtStatus.Value = txtStatus.Value & "Trying to import data from file..." & vbCrLf
            DoCmd.TransferText acImportDelim, "Import_Specification", "Opt_In_Customer_Record", txtFilePath.Value, False
            txtStatus.Value = txtStatus.Value & "Data imported successfully." & vbCrLf
            Testing
            txtStatus.Value = ""
    Else
            MsgBox "File does not exist. Please enter again."
    End If
End If

Exit Sub

Error_Handling:
MsgBox "Error while generating data! Please check your data setting!"
Exit Sub

End Sub


Sub Testing()
'On Error GoTo Error_Handling
   Dim conConnection As New ADODB.Connection
   Dim cmdCommand As New ADODB.Command
   Dim rstRecordSet As New ADODB.Recordset

   Dim eventPlanCode As String
   Dim visitedCountry As String
   Dim startDateTxt As String
   Dim startDate As Date
   Dim endDate As Date
   Dim imsi As String
   Dim currentMonth As String
   Dim nextMonth As String
   Dim currentYear As String
   Dim nextYear As String
   Dim temp As Integer
   Dim sql As String

   'MsgBox CurrentDb.Name

   With conConnection
    .Provider = "Microsoft.Jet.OLEDB.4.0"
    .ConnectionString = CurrentDb.Name
    .Open
   End With

    'MsgBox conConnection.ConnectionString

   With cmdCommand
    .ActiveConnection = conConnection
    .CommandText = "SELECT * FROM Opt_In_Customer_Record;"
    .CommandType = adCmdText
   End With

   With rstRecordSet
    .CursorType = adOpenStatic
    .CursorLocation = adUseClient
    .LockType = adLockOptimistic
    .Open cmdCommand
   End With

   If rstRecordSet.EOF = False Then
        rstRecordSet.MoveFirst
        Do

            'Debug.Print txtStatus.Value
            eventPlanCode = rstRecordSet!Event_Plan_Code
            visitedCountry = rstRecordSet!Visited_Country
            startDateTxt = rstRecordSet!Start_Date
            imsi = rstRecordSet!imsi

            currentMonth = Mid$(startDateTxt, 3, 2) '01
            currentYear = "20" & Mid$(startDateTxt, 5, 2) '2011

            startDate = DateSerial(Val(currentYear), Val(currentMonth), Val(Mid$(startDateTxt, 1, 2)))
            endDate = startDate + Val(Mid$(eventPlanCode, 1, 1))

            MsgBox rstRecordSet!Event_Plan_Code

            If (Mid$(eventPlanCode, 1, 1) = "1") Then
                Exit Sub
            End If

            'MsgBox startDate & " " & endDate
            If (currentMonth = "01") Then
                nextMonth = "02"
            ElseIf (currentMonth = "02") Then
                nextMonth = "03"
            ElseIf (currentMonth = "03") Then
                nextMonth = "04"
            ElseIf (currentMonth = "04") Then
                nextMonth = "05"
            ElseIf (currentMonth = "05") Then
                nextMonth = "06"
            ElseIf (currentMonth = "06") Then
                nextMonth = "07"
            ElseIf (currentMonth = "07") Then
                nextMonth = "08"
            ElseIf (currentMonth = "08") Then
                nextMonth = "09"
            ElseIf (currentMonth = "09") Then
                nextMonth = "10"
            ElseIf (currentMonth = "10") Then
                nextMonth = "11"
            ElseIf (currentMonth = "11") Then
                nextMonth = "12"
            ElseIf (currentMonth = "12") Then
                nextMonth = "01"
            End If

            temp = Val(currentYear)
            temp = temp + 1
            nextYear = Str(temp)

            'MsgBox currentYear & currentMonth & " " & nextYear & nextMonth

            'Exit Do
            rstRecordSet.MoveNext
        Loop Until rstRecordSet.EOF = True
   End If

   'sql = "select * from ( select * from " & "dbo.inbound_rated_all_" & currentYear & currentMonth & " A inner join Opt_In_Customer_Record B "


   conConnection.Close
   Set conConnection = Nothing
   Set cmdCommand = Nothing
   Set rstRecordSet = Nothing

   Exit Sub

Error_Handling:
MsgBox "Error during function Testing!"
Set conConnection = Nothing
Set cmdCommand = Nothing
Set rstRecordSet = Nothing
Exit Sub

End Sub

1 个答案:

答案 0 :(得分:1)

如果要按特定顺序排列行,请在查询中添加ORDER BY子句:

select * from opt_in_customer_record order by event_plan_code

实际上,event_plan_code不是正确的列,因为它包含重复项,但这应该指向正确的方向。