473,549 Members | 2,948 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to export table data into xls or text file using VB6

15 New Member
Hi,


By using VB6.0, I want to export database table data into (i.e based on the selected file type(xls or txt)) excel file or text file with a tab delimited text file.

My User interface has:

• Drop down list box contain list of data base table name.
• A path selection area, allowing the user to specify the filename, path and file type for the
export. This will incorporate standard Windows functionality for allowing the user to specify
file type such as tab delimited text or Excel. The path will default to the xyz server upon
which my project instance is running but will allow the user to navigate to any other mapped drive.

• Export button – when clicked, this activates the data export using the parameters provided.

I think i have to use common dialog box as well. I am new to this job..please give me relevant code...

Thanks in advance
Radhakrishnan
vrradhakrishnan @gmail.com
Sep 2 '06 #1
5 37202
Hemant Pathak
92 Recognized Expert New Member
Sub SaveAsExcel(ByV al rs As DAO.Recordset, ByVal filename
As String, Optional Ffmt As XlFileFormat = xlWorkbookNorma l, _
Optional bHeaders As Boolean = True)
'************** *************** *************** ***************
' Marko Hernandez
' Dec. 2, 2000
'
' Exports a Recordset data into a Microsoft Excel Sheet and
'then can save as new file
' with a given format such Lotus, Q-Pro, dBase, Text
'
' Arguments:
'
' rs : Recordset object (DAO) containing data.
' filename: Name of the file.
' Ffmt: File Format the default value is the
'MS-Excel current version.
' bHeaders: If true the name of the fields will be inserted
'in the first row of each column.
'

Dim xlApp As Excel.Applicati on
Dim xlBook As Excel.Workbook
Dim xlSheet As Excel.Worksheet

'Field object
Dim fd As Field

'Cell count, the cells we can use
Dim CellCnt As Integer

'File Extension Type
Dim Fet As String

Screen.MousePoi nter = vbHourglass
' Assign object references to the variables. Use
' Add methods to create new workbook and worksheet
' objects.
Set xlApp = New Excel.Applicati on
Set xlBook = xlApp.Workbooks .Add
Set xlSheet = xlBook.Workshee ts.Add

'Get the field names
If bHeaders Then
CellCnt = 1
For Each fd In rs.Fields
Select Case fd.Type
Case dbBinary, dbGUID, dbLongBinary, dbVarBinary
' This type of data can't export to excel
Case Else
xlSheet.Cells(1 , CellCnt).Value = fd.Name
xlSheet.Cells(1 , CellCnt).Interi or.ColorIndex = 33
xlSheet.Cells(1 , CellCnt).Font.B old = True
xlSheet.Cells(1 , CellCnt).Border Around xlContinuous
CellCnt = CellCnt + 1
End Select
Next
End If

'Rewind the rescordset
rs.MoveFirst
i = 2
Do While Not rs.EOF()
CellCnt = 1
For Each fd In rs.Fields
Select Case fd.Type
Case dbBinary, dbGUID, dbLongBinary, dbVarBinary
' This type of data can't export to excel
Case Else
xlSheet.Cells(i , CellCnt).Value = _
rs.Fields(fd.Na me).Value
'xlSheet.Column s().AutoFit
CellCnt = CellCnt + 1
End Select
Next
rs.MoveNext
i = i + 1
Loop

'Fit all columns
CellCnt = 1
For Each fd In rs.Fields

Select Case fd.Type
Case dbBinary, dbGUID, dbLongBinary, _
dbVarBinary
' This type of data can't export to excel
Case Else
xlSheet.Columns (CellCnt).AutoF it
CellCnt = CellCnt + 1
End Select
Next

'Get the file extension
Select Case Ffmt
Case xlSYLK
Fet = "slk"
Case xlWKS
Fet = "wks"
Case xlWK1, xlWK1ALL, xlWK1FMT
Fet = "wk1"
Case xlCSV, xlCSVMac, xlCSVdos, xlCSVWindows
Fet = "csv"
Case xlDBF2, xlDBF3, xlDBF4
Fet = "dbf"
Case xlWorkbookNorma l, xlExcel2FarEast , xlExcel3, _
xlExcel4, xlExcel4Workboo k, xlExcel5, xlExcel6, _
xlExcel7, xlExcel9795
Fet = "xls"
Case xlHTML
Fet = "htm"
Case xlTextMac, xlTextdos, xlTextWindows, xlUnicodeText, _
xlCurrentPlatfo rmText
Fet = "txt"
Case xlTextPrinter
Fet = "prn"
Case Else
Fet = "dat"
End Select

' Save the Worksheet.
If InStr(1, filename, ".") = 0 Then filename = _
filename + "." + Fet
xlSheet.SaveAs filename, Ffmt

' Close the Workbook
xlBook.Close
' Close Microsoft Excel with the Quit method.
xlApp.Quit

' Release the objects.
Set xlApp = Nothing
Set xlBook = Nothing
Set xlSheet = Nothing

Screen.MousePoi nter = vbDefault
End Sub
''************* ******USAGE BELOW********** *************
Private Sub Command1_Click( )
SaveAsExcel Data1.Recordset .Clone(), Text1.Text, _
Combo1.ItemData (Combo1.ListInd ex)
'End Sub



Private Sub Form_Load()

Text1.Text = "C:\Export"
Combo1.AddItem "Installed Excel Format"
Combo1.ItemData (Combo1.NewInde x) = xlWorkbookNorma l
Combo1.AddItem "Comma Separated Text"
Combo1.ItemData (Combo1.NewInde x) = xlCSV
Combo1.AddItem "Excel 95/97"
Combo1.ItemData (Combo1.NewInde x) = xlExcel9795
Combo1.AddItem "Internet Format (HTML)"
Combo1.ItemData (Combo1.NewInde x) = xlHtml
Combo1.AddItem "MS-DOS Text"
Combo1.ItemData (Combo1.NewInde x) = xlTextMSDOS
Combo1.AddItem "Lotus 123 (WK1)"
Combo1.ItemData (Combo1.NewInde x) = xlWK1
Combo1.AddItem "Lotus 123 (WKS)"
Combo1.ItemData (Combo1.NewInde x) = xlWKS
Combo1.AddItem "Quattro Pro"
Combo1.ItemData (Combo1.NewInde x) = xlWQ1

Combo1.ListInde x = 0

End Sub
Sep 5 '06 #2
kuleszajmk
1 New Member
This looks cool, for a hack like myself can this code be run on my PC or do I need server access.

James
Nov 11 '06 #3
debuDM
1 New Member
the following code is not running higher version of office software.

Sub SaveAsExcel(ByV al rs As DAO.Recordset, ByVal filename
As String, Optional Ffmt As XlFileFormat = xlWorkbookNorma l, _
Optional bHeaders As Boolean = True)
'************** *************** *************** ***************
' Marko Hernandez
' Dec. 2, 2000
'
' Exports a Recordset data into a Microsoft Excel Sheet and
'then can save as new file
' with a given format such Lotus, Q-Pro, dBase, Text
'
' Arguments:
'
' rs : Recordset object (DAO) containing data.
' filename: Name of the file.
' Ffmt: File Format the default value is the
'MS-Excel current version.
' bHeaders: If true the name of the fields will be inserted
'in the first row of each column.
'

Dim xlApp As Excel.Applicati on
Dim xlBook As Excel.Workbook
Dim xlSheet As Excel.Worksheet

'Field object
Dim fd As Field

'Cell count, the cells we can use
Dim CellCnt As Integer

'File Extension Type
Dim Fet As String

Screen.MousePoi nter = vbHourglass
' Assign object references to the variables. Use
' Add methods to create new workbook and worksheet
' objects.
Set xlApp = New Excel.Applicati on
Set xlBook = xlApp.Workbooks .Add
Set xlSheet = xlBook.Workshee ts.Add

'Get the field names
If bHeaders Then
CellCnt = 1
For Each fd In rs.Fields
Select Case fd.Type
Case dbBinary, dbGUID, dbLongBinary, dbVarBinary
' This type of data can't export to excel
Case Else
xlSheet.Cells(1 , CellCnt).Value = fd.Name
xlSheet.Cells(1 , CellCnt).Interi or.ColorIndex = 33
xlSheet.Cells(1 , CellCnt).Font.B old = True
xlSheet.Cells(1 , CellCnt).Border Around xlContinuous
CellCnt = CellCnt + 1
End Select
Next
End If

'Rewind the rescordset
rs.MoveFirst
i = 2
Do While Not rs.EOF()
CellCnt = 1
For Each fd In rs.Fields
Select Case fd.Type
Case dbBinary, dbGUID, dbLongBinary, dbVarBinary
' This type of data can't export to excel
Case Else
xlSheet.Cells(i , CellCnt).Value = _
rs.Fields(fd.Na me).Value
'xlSheet.Column s().AutoFit
CellCnt = CellCnt + 1
End Select
Next
rs.MoveNext
i = i + 1
Loop

'Fit all columns
CellCnt = 1
For Each fd In rs.Fields

Select Case fd.Type
Case dbBinary, dbGUID, dbLongBinary, _
dbVarBinary
' This type of data can't export to excel
Case Else
xlSheet.Columns (CellCnt).AutoF it
CellCnt = CellCnt + 1
End Select
Next

'Get the file extension
Select Case Ffmt
Case xlSYLK
Fet = "slk"
Case xlWKS
Fet = "wks"
Case xlWK1, xlWK1ALL, xlWK1FMT
Fet = "wk1"
Case xlCSV, xlCSVMac, xlCSVdos, xlCSVWindows
Fet = "csv"
Case xlDBF2, xlDBF3, xlDBF4
Fet = "dbf"
Case xlWorkbookNorma l, xlExcel2FarEast , xlExcel3, _
xlExcel4, xlExcel4Workboo k, xlExcel5, xlExcel6, _
xlExcel7, xlExcel9795
Fet = "xls"
Case xlHTML
Fet = "htm"
Case xlTextMac, xlTextdos, xlTextWindows, xlUnicodeText, _
xlCurrentPlatfo rmText
Fet = "txt"
Case xlTextPrinter
Fet = "prn"
Case Else
Fet = "dat"
End Select

' Save the Worksheet.
If InStr(1, filename, ".") = 0 Then filename = _
filename + "." + Fet
xlSheet.SaveAs filename, Ffmt

' Close the Workbook
xlBook.Close
' Close Microsoft Excel with the Quit method.
xlApp.Quit

' Release the objects.
Set xlApp = Nothing
Set xlBook = Nothing
Set xlSheet = Nothing

Screen.MousePoi nter = vbDefault
End Sub
''************* ******USAGE BELOW********** *************
Private Sub Command1_Click( )
SaveAsExcel Data1.Recordset .Clone(), Text1.Text, _
Combo1.ItemData (Combo1.ListInd ex)
'End Sub



Private Sub Form_Load()

Text1.Text = "C:\Export"
Combo1.AddItem "Installed Excel Format"
Combo1.ItemData (Combo1.NewInde x) = xlWorkbookNorma l
Combo1.AddItem "Comma Separated Text"
Combo1.ItemData (Combo1.NewInde x) = xlCSV
Combo1.AddItem "Excel 95/97"
Combo1.ItemData (Combo1.NewInde x) = xlExcel9795
Combo1.AddItem "Internet Format (HTML)"
Combo1.ItemData (Combo1.NewInde x) = xlHtml
Combo1.AddItem "MS-DOS Text"
Combo1.ItemData (Combo1.NewInde x) = xlTextMSDOS
Combo1.AddItem "Lotus 123 (WK1)"
Combo1.ItemData (Combo1.NewInde x) = xlWK1
Combo1.AddItem "Lotus 123 (WKS)"
Combo1.ItemData (Combo1.NewInde x) = xlWKS
Combo1.AddItem "Quattro Pro"
Combo1.ItemData (Combo1.NewInde x) = xlWQ1

Combo1.ListInde x = 0

End Sub
Apr 25 '07 #4
coolminded
137 New Member
Sub SaveAsExcel(ByV al rs As DAO.Recordset, ByVal filename
As String, Optional Ffmt As XlFileFormat = xlWorkbookNorma l, _
Optional bHeaders As Boolean = True)
'************** *************** *************** ***************
' Marko Hernandez
' Dec. 2, 2000
'
' Exports a Recordset data into a Microsoft Excel Sheet and
'then can save as new file
' with a given format such Lotus, Q-Pro, dBase, Text
'
' Arguments:
'
' rs : Recordset object (DAO) containing data.
' filename: Name of the file.
' Ffmt: File Format the default value is the
'MS-Excel current version.
' bHeaders: If true the name of the fields will be inserted
'in the first row of each column.
'

Dim xlApp As Excel.Applicati on
Dim xlBook As Excel.Workbook
Dim xlSheet As Excel.Worksheet

'Field object
Dim fd As Field

'Cell count, the cells we can use
Dim CellCnt As Integer

'File Extension Type
Dim Fet As String

Screen.MousePoi nter = vbHourglass
' Assign object references to the variables. Use
' Add methods to create new workbook and worksheet
' objects.
Set xlApp = New Excel.Applicati on
Set xlBook = xlApp.Workbooks .Add
Set xlSheet = xlBook.Workshee ts.Add

'Get the field names
If bHeaders Then
CellCnt = 1
For Each fd In rs.Fields
Select Case fd.Type
Case dbBinary, dbGUID, dbLongBinary, dbVarBinary
' This type of data can't export to excel
Case Else
xlSheet.Cells(1 , CellCnt).Value = fd.Name
xlSheet.Cells(1 , CellCnt).Interi or.ColorIndex = 33
xlSheet.Cells(1 , CellCnt).Font.B old = True
xlSheet.Cells(1 , CellCnt).Border Around xlContinuous
CellCnt = CellCnt + 1
End Select
Next
End If

'Rewind the rescordset
rs.MoveFirst
i = 2
Do While Not rs.EOF()
CellCnt = 1
For Each fd In rs.Fields
Select Case fd.Type
Case dbBinary, dbGUID, dbLongBinary, dbVarBinary
' This type of data can't export to excel
Case Else
xlSheet.Cells(i , CellCnt).Value = _
rs.Fields(fd.Na me).Value
'xlSheet.Column s().AutoFit
CellCnt = CellCnt + 1
End Select
Next
rs.MoveNext
i = i + 1
Loop

'Fit all columns
CellCnt = 1
For Each fd In rs.Fields

Select Case fd.Type
Case dbBinary, dbGUID, dbLongBinary, _
dbVarBinary
' This type of data can't export to excel
Case Else
xlSheet.Columns (CellCnt).AutoF it
CellCnt = CellCnt + 1
End Select
Next

'Get the file extension
Select Case Ffmt
Case xlSYLK
Fet = "slk"
Case xlWKS
Fet = "wks"
Case xlWK1, xlWK1ALL, xlWK1FMT
Fet = "wk1"
Case xlCSV, xlCSVMac, xlCSVdos, xlCSVWindows
Fet = "csv"
Case xlDBF2, xlDBF3, xlDBF4
Fet = "dbf"
Case xlWorkbookNorma l, xlExcel2FarEast , xlExcel3, _
xlExcel4, xlExcel4Workboo k, xlExcel5, xlExcel6, _
xlExcel7, xlExcel9795
Fet = "xls"
Case xlHTML
Fet = "htm"
Case xlTextMac, xlTextdos, xlTextWindows, xlUnicodeText, _
xlCurrentPlatfo rmText
Fet = "txt"
Case xlTextPrinter
Fet = "prn"
Case Else
Fet = "dat"
End Select

' Save the Worksheet.
If InStr(1, filename, ".") = 0 Then filename = _
filename + "." + Fet
xlSheet.SaveAs filename, Ffmt

' Close the Workbook
xlBook.Close
' Close Microsoft Excel with the Quit method.
xlApp.Quit

' Release the objects.
Set xlApp = Nothing
Set xlBook = Nothing
Set xlSheet = Nothing

Screen.MousePoi nter = vbDefault
End Sub
''************* ******USAGE BELOW********** *************
Private Sub Command1_Click( )
SaveAsExcel Data1.Recordset .Clone(), Text1.Text, _
Combo1.ItemData (Combo1.ListInd ex)
'End Sub



Private Sub Form_Load()

Text1.Text = "C:\Export"
Combo1.AddItem "Installed Excel Format"
Combo1.ItemData (Combo1.NewInde x) = xlWorkbookNorma l
Combo1.AddItem "Comma Separated Text"
Combo1.ItemData (Combo1.NewInde x) = xlCSV
Combo1.AddItem "Excel 95/97"
Combo1.ItemData (Combo1.NewInde x) = xlExcel9795
Combo1.AddItem "Internet Format (HTML)"
Combo1.ItemData (Combo1.NewInde x) = xlHtml
Combo1.AddItem "MS-DOS Text"
Combo1.ItemData (Combo1.NewInde x) = xlTextMSDOS
Combo1.AddItem "Lotus 123 (WK1)"
Combo1.ItemData (Combo1.NewInde x) = xlWK1
Combo1.AddItem "Lotus 123 (WKS)"
Combo1.ItemData (Combo1.NewInde x) = xlWKS
Combo1.AddItem "Quattro Pro"
Combo1.ItemData (Combo1.NewInde x) = xlWQ1

Combo1.ListInde x = 0

End Sub

hi Hemant
can u help my on how to import data from excel to vb
plz, asap
Apr 27 '07 #5
Thiem Teddy
2 New Member
Dear Hemant Pathak
I tried to execute your program but i don't know what kind of libraries that i need to add into your program. pls you tell me how many libraries and waht kind of libraries i have to add in?

because i am a new in VB6
thanks for your contribution
Mar 30 '11 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

1
3109
by: Ward B | last post by:
Greetings. I'm somewhat new to this whole MySQL/PHP thing and need a little help. My web hosting service uses phpMyAdmin and at the bottom of the screen iis an area where I can upload a text file to populate a table. I have a table named groups with two fields: groups_id auto-increment primary groups_name
2
7226
by: Pecanfan | last post by:
Hi, I'm trying to come up with a way of generating 'warning' e-mails based on specific criteria within an Access (2003) database. I've decided to do this by exporting specific information to a text file which then gets parsed by a simple shell program and processed by a 3rd party SMTP engine (Blat). With my VERY limited knowledge of VB...
10
14716
by: Neil | last post by:
Hi guyz, just trying out this google feature so if i post if in the wrong area i appologize. now to my question. BTW i'm new to access programming, i've done a little vb6.0 and vb.net but access vba is a little confusing for me since i have never used it plus i have lost touch with vb coding since last 8 monhts. anywayz, my problem is i'm...
1
2086
by: raju5725 | last post by:
I have a MS access table and I want to export it to comma delimited text file. How do I do this programmatically using VB.NET or C#? Thanks for any help in advance. Raju
4
8165
by: raju5725 | last post by:
have a MS access table and I want to export it to comma delimited text file. How do I do this programmatically using VB.NET or C#? Thanks for any help in advance. Raju
0
953
by: raju5725 | last post by:
have a MS access table and I want to export it to comma delimited text file. How do I do this programmatically using VB.NET? Thanks for any help in advance. Raju
1
2183
by: christine0207 | last post by:
Hi, how to generate a fixed length data text file using Scripting.TextStream / filesystemobject.
9
7685
by: NEWSGROUPS | last post by:
I have data in a table in an Access 2000 database that needs to be exported to a formatted text file. For instance, the first field is an account number that is formatted in the table as text and is 8 characters long. This field needs to be exported as pic(15) padded in the front with 0's (zeros). The next field an ID name that is 15...
3
10935
by: Wayne | last post by:
I'm trying to automate the export of a query to a text file using code. If I export the query manually I get the exact result that I want i.e. If I select the query then choose File/Export from the menu bar, choose "Text" as the save format, and then select "fixed width" as the export format, I end up with a fixed width text file with no...
0
1329
by: Thiem Teddy | last post by:
Dear all I use VB6 to export data into xls or text file, I followed this example ( http://bytes.com/topic/visual-basic/answers/530866-how-export-table-data-into-xls-text-file-using-vb6 ). but I don't know what kind of libraries that i need to add into this program because i'm a new in VB6 may you tell me how many libraries i need to add...
0
7520
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7446
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7956
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7470
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6041
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5088
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3498
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3480
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1058
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.