Consulting

Results 1 to 6 of 6

Thread: VBA function to return MySQL Data

  1. #1
    VBAX Newbie
    Joined
    Aug 2017
    Posts
    3
    Location

    Question VBA function to return MySQL Data

    Hello,

    This is my first post here so hoping you can help.

    I need a function to return a dataset to a Sub. What am I doing wrong?

    Sub data()
    Dim SQLstr As String
    Dim rst As String
    Dim ir As Integer
    Dim ic As Integer
    
    
    
    
    
    
    SQLstr = "SELECT * FROM machines;"
    rst = connect(SQLstr)
    
    
    ir = 1
        
        While Not rst.EOF
        For iCols = 0 To rst.Fields.Count - 1
            Worksheets("Sheet1").Cells(ir, ic + 1).Value = rst.Fields(ic).Value
        Next
        rst.MoveNext
        ir = ir + 1
        Wend
    
    
    
    
    End Sub
    
    
    
    
    Public Function connect(SQLstr As String)
        Dim Password As String
        Dim Server_Name As String
        Dim User_ID As String
        Dim Database_Name As String
    
    
        
        Set rst = CreateObject("ADODB.Recordset")
        Server_Name = "" 'IP ADDRESS HIDDEN
        Database_Name = "" 'DATABASE HIDDEN
        User_ID = "" ' USER HIDDEN
        Password = "" ' PASSWORD HIDDEN
       
        
        Set cn = CreateObject("ADODB.Connection")
        cn.Open "Driver={MySQL ODBC 5.3 Unicode Driver};Server=" & Server_Name & ";Database=" & Database_Name & ";Uid=" & User_ID & ";Pwd=" & Password & ";"
        
        rst.Open SQLstr, cn, adopenstatic
       
       
        
    Return connect
    rst.Close
    Set rst = Nothing
      cn.Close
       
    End Function

  2. #2
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    how about trying mysql's excel add-in?
    https://dev.mysql.com/downloads/windows/excel/
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  3. #3
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    You close the recordset in the function, so you cannot access it outside. Why not just drop it into an array and return that array, something like

     
    Sub data()
        Dim SQLstr As String
        Dim rst As Variant '<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
        Dim ir As Integer
        Dim ic As Integer
         
        SQLstr = "SELECT * FROM machines;"
        rst = GetData(SQLstr)
          
        ir = 1
         
        For ir = LBound(rst, 1) To UBound(rst, 1)
    
            For ic = LBound(rst, 2) To UBound(rst, 2)
            
                Worksheets("Sheet1").Cells(ir, ic + 1).Value = rst(ir, ic)
            Next ic
        Next ir
    End Sub
     
    Public Function GetData(SQLstr As String) As Variant '<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
        Dim Password As String
        Dim Server_Name As String
        Dim User_ID As String
        Dim Database_Name As String
        
        Set rst = CreateObject("ADODB.Recordset")
        Server_Name = "" 'IP ADDRESS HIDDEN
        Database_Name = "" 'DATABASE HIDDEN
        User_ID = "" ' USER HIDDEN
        Password = "" ' PASSWORD HIDDEN
         
        Set cn = CreateObject("ADODB.Connection")
        cn.Open "Driver={MySQL ODBC 5.3 Unicode Driver};Server=" & Server_Name & ";Database=" & Database_Name & ";Uid=" & User_ID & ";Pwd=" & Password & ";"
         
        rst.Open SQLstr, cn, adopenstatic
        GetData = rst.GetRows  '<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
        rst.Close
        Set rst = Nothing
        cn.Close
         
    End Function
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  4. #4
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,872
    Also I don't think vba supports
    Return connect
    I think it's more likely to be
    connect = something-or-other
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  5. #5
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    I should also have mentioned that you can dump that array into the worksheet range directly, no looping, or even use the CopyFromRecordset method to drop the recordset into a range.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  6. #6
    VBAX Newbie
    Joined
    Aug 2017
    Posts
    3
    Location
    Quote Originally Posted by xld View Post
    You close the recordset in the function, so you cannot access it outside. Why not just drop it into an array and return that array, something like

     
    Sub data()
        Dim SQLstr As String
        Dim rst As Variant '<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
        Dim ir As Integer
        Dim ic As Integer
         
        SQLstr = "SELECT * FROM machines;"
        rst = GetData(SQLstr)
          
        ir = 1
         
        For ir = LBound(rst, 1) To UBound(rst, 1)
    
            For ic = LBound(rst, 2) To UBound(rst, 2)
            
                Worksheets("Sheet1").Cells(ir, ic + 1).Value = rst(ir, ic)
            Next ic
        Next ir
    End Sub
     
    Public Function GetData(SQLstr As String) As Variant '<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
        Dim Password As String
        Dim Server_Name As String
        Dim User_ID As String
        Dim Database_Name As String
        
        Set rst = CreateObject("ADODB.Recordset")
        Server_Name = "" 'IP ADDRESS HIDDEN
        Database_Name = "" 'DATABASE HIDDEN
        User_ID = "" ' USER HIDDEN
        Password = "" ' PASSWORD HIDDEN
         
        Set cn = CreateObject("ADODB.Connection")
        cn.Open "Driver={MySQL ODBC 5.3 Unicode Driver};Server=" & Server_Name & ";Database=" & Database_Name & ";Uid=" & User_ID & ";Pwd=" & Password & ";"
         
        rst.Open SQLstr, cn, adopenstatic
        GetData = rst.GetRows  '<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
        rst.Close
        Set rst = Nothing
        cn.Close
         
    End Function
    Thank you very much that will work perfectly.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •