2011-03-16 39 views
0

我正在製作一個.MDB文件,其中包括一個ms訪問數據庫和一個使用vb 6創建的表單。我使用ms access 2000,並且需要連接到MDB中的本地數據庫,和一個遠程MS SQL 2005數據庫。在ADO中連接和查詢時遇到的問題

在下面的代碼中,我可以使用msgbox來顯示結果集中的返回值,但是當嘗試在文本框中輸出時,例如:txtStatus.Value = txtStatus.Value & rstRecordSet.Fields(1) & vbCrLf,它只是掛起。從教程的原始示例中顯示的方法得到了Debug.Print方法,但事實證明,我沒有看到任何內容。我的意思是,VB沒有控制檯面板,打印語句將在哪裏進行?

與遇到錯誤代碼:

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

    conConnection.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & _ 
    App.Path & "\" & CurrentDb.Name & ";" 
    conConnection.CursorLocation = adUseClient 

    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 
      MsgBox "Record " & rstRecordSet.AbsolutePosition & " " & _ 
      rstRecordSet.Fields(0).Name & "=" & rstRecordSet.Fields(0) & " " & _ 
      rstRecordSet.Fields(1).Name & "=" & rstRecordSet.Fields(1) 
      rstRecordSet.MoveNext 
     Loop Until rstRecordSet.EOF = True 
    End If 

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

    Exit Function 

Error_Handling: 
MsgBox "Error during function Testing!" 
Exit Function 

End Function 
+1

你可以列出的錯誤,併線是發生哪些? – JeffO 2011-03-16 15:18:43

回答

1

我認爲這是在開始;-) 無論如何,我認爲你是在談論ADO,在您的標題一個笑話。

Hereyoucan找東西。 這個site將幫助您處理不同數據庫的連接字符串。
訪問和使用ADO的sql服務器之間的區別正是連接字符串。 我建議你避免使用遠程數據控制,因爲開始時會讓你的生活變得更簡單,但是你必須與他們抗爭,因爲他們不能正常工作。

這是連接的實例和數據的獲取:

Dim cnn As New ADODB.Connection 
Dim cmd As New ADODB.Command 
Dim strSql As String 

cnn.ConnectionString = _ 
    "Provider=Microsoft.Jet.OLEDB.4.0;" & _ 
    "Data Source=m:\testdbSource\testSource.mdb;" & _ 
    "User Id=admin;Password=;" 
cnn.Open 

cmd.ActiveConnection = cnn 
cmd.CommandType = adCmdText 
cmd.CommandText = "select * from tblSource" 
cmd.Execute 

Set cmd = Nothing 
cnn.Close 
Set cnn = Nothing 

此樣品爲我的作品:

Function Testing() 

    On Error GoTo Error_Handling 

    Dim MyDb As String 
    Dim conConnection As New ADODB.Connection 
    Dim cmdCommand As New ADODB.Command 
    Dim rstRecordSet As New ADODB.Recordset 

    MyDb = "db1.mdb" 

    With conConnection 
     .Provider = "Microsoft.Jet.OLEDB.4.0" 
     .ConnectionString = App.Path & "\" & MyDb 
     .Open 
    End With 


    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 

    Do While Not rstRecordSet.EOF 
     MsgBox "Record " & rstRecordSet.AbsolutePosition & " " & _ 
      rstRecordSet.Fields(0).Name & "=" & rstRecordSet.Fields(0) & " " & _ 
      rstRecordSet.Fields(1).Name & "=" & rstRecordSet.Fields(1) 
      rstRecordSet.MoveNext 
    Loop 

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

    Exit Function 

Error_Handling: 
    MsgBox "Error during function Testing!" 
    MsgBox Err.Description 

End Function 
+0

如何檢查錯誤?我知道有錯誤,因爲它跳轉到我的錯誤處理部分,並說錯誤,但我不知道它是什麼。 – lamwaiman1988 2011-03-16 09:44:04

+0

我已經在我的問題中發佈我的代碼。我試圖連接到我的MDB文件中的數據庫,並進行查詢,但不知何故失敗。請幫忙。 – lamwaiman1988 2011-03-16 09:50:59

+0

您是否參考過「Activex數據對象2.x」? – LeftyX 2011-03-16 10:13:58