Results 1 to 6 of 6
  1. #1
    shabbaranks is offline Competent Performer
    Windows 7 32bit Access 2007
    Join Date
    Oct 2011
    Posts
    162

    Trying to export MySql/VBA to csv - cant get it working??

    Hi all,



    I managed to export to csv using a query but Im trying to tie it all into VBA so users cant run the query. My code thus far is:

    Code:
        MySql = "SELECT TimesheetTable.ID, TimesheetTable.sUser, UserNames_tbl.WorksNumber, TimesheetTable.Activity, TimesheetTable.Hours, TimesheetTable.Project, TimesheetTable.[Task Date], TimesheetTable.Description, TimesheetTable.Approved "
        MySql = MySql & "FROM UserNames_tbl INNER JOIN TimesheetTable ON UserNames_tbl.sUser = TimesheetTable.sUser"
     
    Docmd.OutputTo acOutputFunction, “N:\TimesheetDatabase\” & Format(Date, “ddmmyyyy”) & “.csv", True
    End Sub
        Else
            MsgBox "Sorry, you do not have access to this function", _
                   vbOKOnly, "Important Information"
            Exit Sub
        End If
    but I cant get it workin - any ideas please?

    Thanks in advance

  2. #2
    ssanfu is offline Master of Nothing
    Windows 2K Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    Quote Originally Posted by shabbaranks View Post
    Hi all,

    I managed to export to csv using a query but Im trying to tie it all into VBA so users cant run the query. My code thus far is:

    Code:
        MySql = "SELECT TimesheetTable.ID, TimesheetTable.sUser, UserNames_tbl.WorksNumber, TimesheetTable.Activity, TimesheetTable.Hours, TimesheetTable.Project, TimesheetTable.[Task Date], TimesheetTable.Description, TimesheetTable.Approved "
        MySql = MySql & "FROM UserNames_tbl INNER JOIN TimesheetTable ON UserNames_tbl.sUser = TimesheetTable.sUser"
     
    Docmd.OutputTo acOutputFunction, “N:\TimesheetDatabase\” & Format(Date, “ddmmyyyy”) & “.csv", True
    End Sub
        Else
            MsgBox "Sorry, you do not have access to this function", _
                   vbOKOnly, "Important Information"
            Exit Sub
        End If
    but I cant get it workin - any ideas please?

    Thanks in advance
    It would be nice to see more of the code, but.........

    Looking at what you have provided, it appears that "MySQL" is declared as a type String. You created the SQL string, but you didn't open the recordset. There is nothing to output


    Also, I think the syntax is wrong. The syntax is:

    DoCmd.OutputTo objecttype[, objectname][, outputformat][, outputfile][, autostart][, templatefile]

    For objecttype, I couldn't find a type of "acOutputFunction"
    for objectname, there is no table/query name to get the records from
    for outputformat , there is no format specified.


    Why is "End Sub" after the DoCmd" line?? There should only be ONE "End Sub" in any subroutine. This is different than an "Exit Sub".

  3. #3
    shabbaranks is offline Competent Performer
    Windows 7 32bit Access 2007
    Join Date
    Oct 2011
    Posts
    162
    WOuld you happen to have any examples of how to achieve this? I think I need to use querydef - what ever that is? Thanks

  4. #4
    orange's Avatar
    orange is offline Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,849

  5. #5
    shabbaranks is offline Competent Performer
    Windows 7 32bit Access 2007
    Join Date
    Oct 2011
    Posts
    162
    Thanks, its working now. I have been looking at examples and trying to get my head around it - slowly but surely its sinking in.

  6. #6
    ssanfu is offline Master of Nothing
    Windows 2K Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    Here is another way: (air code)
    Code:
    Public Sub test_ss()
       Dim rst As DAO.Recordset
       Dim MySQL As String
       Dim SaveToFile As String
    
       '.
       '.your other code
       '.
       '
       MySQL = "SELECT TimesheetTable.ID, TimesheetTable.sUser, UserNames_tbl.WorksNumber,"
       MySQL = MySQL & " TimesheetTable.Activity, TimesheetTable.Hours, TimesheetTable.Project,"
       MySQL = MySQL & " TimesheetTable.[Task Date], TimesheetTable.Description, TimesheetTable.Approved"
       MySQL = MySQL & " FROM UserNames_tbl INNER JOIN TimesheetTable ON UserNames_tbl.sUser = TimesheetTable.sUser"
    
       'open the recordset
       Set rst = CurrentDb.OpenRecordset(MySQL)
       If rst.BOF And rst.EOF Then
          'no records returned
          MsgBox "Sorry, you do not have access to this function", vbOKOnly, "Important Information"
       Else
          ' create save to file name
          SaveToFile = "N:\TimesheetDatabase\" & Format(Date, “ddmmyyyy”) & ".csv"
          DoCmd.OutputTo acOutputQuery, rst, acFormatTXT, SaveToFile, True
    
       End If
    
       'clean up
       rst.Close
       Set rst = Nothing
    
    End Sub

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Export to txt not working
    By Michael T in forum Import/Export Data
    Replies: 6
    Last Post: 12-01-2011, 05:00 AM
  2. Export MySQL database into Access
    By Yesideez in forum Import/Export Data
    Replies: 6
    Last Post: 08-08-2011, 12:13 PM
  3. MySQL & Access
    By Scorpio11 in forum Database Design
    Replies: 3
    Last Post: 05-21-2011, 02:32 PM
  4. Using DSN alternatives with MYSQL
    By ajetrumpet in forum Access
    Replies: 2
    Last Post: 10-14-2010, 12:55 PM
  5. Export to MySQL
    By avincent in forum Import/Export Data
    Replies: 1
    Last Post: 12-19-2009, 09:48 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums