MySql en vba

Status
Niet open voor verdere reacties.

promoboy

Gebruiker
Lid geworden
5 feb 2012
Berichten
14
Ik heb volgend script en ik krijg een error 3001 op lijn "rs.Open strSQL, oConn, adOpenDynamic, adLockOptimistic"
Code:
Private Sub InsertData()
Dim rs As ADODB.Recordset
    Set rs = New ADODB.Recordset
    ConnectDB
    With wsScores
        For rowCursor = 2 To 15
            strSQL = "INSERT INTO scorebord (tornooi, figuur, poule, naam, punten, pogingen) " & _
                "VALUES ('" & esc(.Cells(rowCursor, 2)) & "', " & _
                "'" & esc(.Cells(rowCursor, 3)) & "', " & _
                "'" & esc(.Cells(rowCursor, 4)) & "', " & _
                "'" & esc(.Cells(rowCursor, 5)) & "', " & _
                "'" & esc(.Cells(rowCursor, 6)) & "', " & _
                "'" & esc(.Cells(rowCursor, 7)) & "')"
                MsgBox strSQL
            rs.Open strSQL, oConn, adOpenDynamic, adLockOptimistic
        Next
    End With
End Sub

wat doe ik fout?
 
Moeilijk te zeggen. loopt het al vast op de eerste poging? of lukt er wel 1 insert en dan loopt het vast?

oConn is ook niet lokaal gedefinieerd. Mogelijk is dat een global die gezet wordt door "connectDB" maar voor ons ondoorgrondelijk. Ik zou het persoonlijk eerst test op iets eenvoudigers met een test database, dan kun je ook 1 voor 1 door de problemen heenlopen.
 
Dit is de rest van de code

Code:
Function esc(txt As String)
    esc = Trim(Replace(txt, "'", "\'"))
End Function




Sub ConnectDB()

    Dim myRecordSet As ADODB.Recordset
    Dim conn As ADODB.Connection
    
    Dim ServerName As String
    Dim DatabaseName As String
    Dim UserID As String
    Dim Password As String
    Dim SqlQuery As String
    Dim ConnectionString As String
    
    '-----------------------------------------------------------------
    '  All this information would be unique to your DB Connection
    '-----------------------------------------------------------------
    
    ServerName = "***********"
    DatabaseName = "********"
    UserID = "*********"
    Password = "**********"
    
    '-----------------------------------------------------------------
    '  define your connection string
    '-----------------------------------------------------------------
    ConnectionString = "Driver={MySQL ODBC 5.3.4 Driver};" & _
                       "Server=" & ServerName & ";" & _
                       "Database=" & DatabaseName & ";" & _
                       "Uid=" & UserID & ";" & _
                       "Pwd=" & Password & ";"
    
End Sub
 
Laatst bewerkt:
Ik zou eens een breakpoint zetten en kijken of oConn wel een waarde heeft. Als dit alle code is die je hebt vermoed ik dat oConn niet magisch gevuld wordt vanuit je subroutine. Daarom kun je beter, zoals ik al aangaf in de vorige post eerst het even op een simpele manier in een enkele routine testen.
 
Ik ben terug opnieuw begonnen want ik kon niet vinden wat het probleem juist was

Nu krijg ik een autmatiseringsfout op volgende lijn:
connection.Execute commando_SQL

waarschijnlijk ligt het aan "connection.Execute" maar weet niet wat hieraan te verhelpen
als ik een MsgBox zet op "commando_SQL" krijg ik een goede weergave van wat er in de de DB moet

Mijn code is nu
in een Module
Code:
Function esc(txt As String)
    esc = Trim(Replace(txt, "'", "\'"))
End Function


Option Explicit
Global connection As New ADODB.connection
Global consult As Recordset

Sub connection_DB()
Set connection = New ADODB.connection
Dim ServerName As String
    Dim DatabaseName As String
    Dim UserID As String
    Dim Password As String
'------------------------------------------
    ServerName = "servernaam"
    DatabaseName = "---------_scorebord"
    UserID = "--------_score"
    Password = "*******"
'------------------------------------------
connection.ConnectionString = "DRIVER={MySQL ODBC 3.51 Driver};" _
                       & "SERVER=" & ServerName & ";" _
                       & "DATABASE=" & DatabaseName & ";" _
                       & "UID=" & UserID & ";" _
                       & "PASSWORD=" & Password & ";"
                       
connection.Open
End Sub

Sub closeConnection_DB()

connection.Close
Set connection = Nothing

End Sub

en dit in een sheet
Code:
Private Sub InsertData()

Dim commando_SQL As String

   Call connection_DB
    With wsScores
        For rowCursor = 2 To 15
            commando_SQL = "INSERT INTO scorebord (tornooi, figuur, poule, naam, punten, pogingen) " & _
                "VALUES ('" & esc(.Cells(rowCursor, 2)) & "', " & _
                "'" & esc(.Cells(rowCursor, 3)) & "', " & _
                "'" & esc(.Cells(rowCursor, 4)) & "', " & _
                "'" & esc(.Cells(rowCursor, 5)) & "', " & _
                "'" & esc(.Cells(rowCursor, 6)) & "', " & _
                esc(.Cells(rowCursor, 7)) & ")"
                'MsgBox commando_SQL
            'rs.Open strSQL, oConn, adOpenDynamic, adLockOptimistic
            connection.Execute commando_SQL
        Next
    End With
MsgBox "toegevoegd"
 Call closeConnection_DB
    
End Sub
 
Wordt "connection" herkent als de juiste global? ik weet niet zeker of een global in een module wel doorgezet wordt op een sheet
 
Status
Niet open voor verdere reacties.
Terug
Bovenaan Onderaan