WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

OLE with Access

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

Access Recordset to CSV File UDF


#DefineFunction AccessRSToCSV(dbname, sqlstr, hdr)
      listvar = ""
      accessDB = ObjectOpen("Access.Application")
      ADB = accessDB.Application
      ADB.OpenCurrentDatabase(dbname)
      ADB.Visible = @FALSE
      db = accessDB.currentdb
      rs = db.openrecordset(sqlstr)
      flds = rs.fields
      If hdr
         hstr = ""
         For x = 0 To flds.count-1
            thisfld = rs.fields(x)
            hstr    = ItemInsert(thisfld.name, -1, hstr, ",")
            ObjectClose(thisfld)
         Next
         listvar = StrCat(hstr, @CRLF, listvar)
      EndIf
      While !rs.eof
         Yields(200)
         For x = 0 To flds.count-1
            thisfld = rs.fields(x)
            listvar = StrCat(listvar, thisfld.value, ",")
            ObjectClose(thisfld)
         Next
         rs.movenext
         listvar = StrSub(listvar, 1, StrLen(listvar)-1)
         listvar = StrCat(listvar, @CRLF)
      EndWhile
      rs.close
      ObjectClose(rs)
      ObjectClose(flds)
      ObjectClose(db)
      ObjectClose(ADB)
      ObjectClose(accessDB)
      Return(listvar)
   #EndFunction
;
   dbname = "C:\Data\Access\NWIND.MDB"
   sqlstr = "select [FirstName], [LastName], [EmployeeID] from [Employees]"
   csv = AccessRSToCSV(dbname, sqlstr, @TRUE)
   outputfile = "c:\NorthwindEmployees.txt"
   If FileExist(outputfile) Then FileDelete(outputfile)
   FilePut(outputfile, csv)
Return

Article ID:   W16592
File Created: 2005:02:18:12:21:34
Last Updated: 2005:02:18:12:21:34