Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Visual Basic > VB 6 Visual Basic 6 > VB Databases Basics
|
VB Databases Basics Beginning-level VB coding questions specific to using VB with databases. Issues not specific to database use will be redirected to other forums.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the VB Databases Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old October 23rd, 2003, 01:59 PM
Authorized User
 
Join Date: Jun 2003
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default generating a fixed length export file

I am trying to generate a fixed field lenght file from a database. i have the code to get a comma delimited file but the client wants a fixed length.
Is there something i can change in the current code that i have to get the proper file?
the code i am using is from the Beginning VB6 Database book, page 770-772. see below
Thanks for anyhelp
doug

Public Function csv(adoRecordset As ADODB.Recordset) As Boolean
Dim iTotalRecords As Integer
Dim sFileToExport As String
Dim iFileNum As Integer
Dim msg As String
Dim iIndx As Integer
Dim iNumberOfFields As Integer

 Screen.MousePointer = vbDefault

 On Error Resume Next

 With CD1
 .CancelError = True
 .FileName = "Export.csv"
 .InitDir = App.Path
 .DialogTitle = "Save Comma Delimited Export File"
 .Filter = "Export Files (*.CSV)|*.CSV"
 .DefaultExt = "CSV"
 '.Flags = cd1OFNOverwritePrompt Or cd1OFNCreatePrompt
 .ShowSave
 End With

 If (Err = 32755) Then
 Screen.MousePointer = vbDefault
 Beep
 msg = "the export operation was canceled." & vbCrLf
 iIndx = MsgBox(msg, vbOKOnly + vbInformation, "comma Deleimited Export File")
 csv = False
 Exit Function
 Else
 On Error GoTo experror
 End If


 Screen.MousePointer = vbHourglass
 iTotalRecords = 0
 sFileToExport = CD1.FileName
 iFileNum = FreeFile()
 Open sFileToExport For Output As #iFileNum

 iNumberOfFields = adoRecordset.Fields.Count - 1

 adoRecordset.MoveFirst
 Do Until adoRecordset.EOF
 iTotalRecords = iTotalRecords + 1
 'For iIndx = 0 To iNumberOfFields
 'If (IsNull(adoRecordset.Fields(iIndx))) Then
' Print #iFileNum, ",";
 'Else
 'If iIndx = iNumberOfFields Then
 'Print #iFileNum, Trim$(CStr(adoRecordset.Fields(iIndx)));
 'Else
 ' Print #iFileNum, Trim$(CStr(adoRecordset.Fields(iIndx))); ",";
 'End If
 'End If
 'Next

 Print #iFileNum,
 adoRecordset.MoveNext
 DoEvents
 Loop

 Close iFileNum
 Screen.MousePointer = vbDefault
 Beep
 msg = "Export File " & sFileToExport & vbCrLf
 msg = msg & "successfully created." & vbCrLf
 msg = msg & iTotalRecords & " records written to disk." & vbCrLf
 iIndx = MsgBox(msg, vbOKOnly + vbInformation, "Comma delimited file")

 csv = True
 Exit Function

experror:

 Screen.MousePointer = vbDefault
 MsgBox (Err & " " & Err.Description)
 csv = False

 End Function
 
Old October 24th, 2003, 07:26 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,212
Thanks: 0
Thanked 1 Time in 1 Post
Default

Well you can just change the bits you've commented out. No-one is going to be able to tell you what you need to change it to unless you say what format you need - just saying fixed-length isn't very helpful. We need to know:
-how many fields?
-what length should each field be fixed to?
-if a string is shorter than its allocated length should it be padded wth spaces to the left or to the right?
-if a number is to be written, should it be padded with leading-zeros or spaces?

rgds
Phil
 
Old October 27th, 2003, 03:15 PM
Authorized User
 
Join Date: Jun 2003
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Each field will have a different length depending on how the database fields were set. i want to be able to pull the data just like i was on the SQL server and going to data transformation services and exporting data to a flat file.
 
Old October 27th, 2003, 03:42 PM
sal sal is offline
Friend of Wrox
 
Join Date: Oct 2003
Posts: 702
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Create a DTS package to do exactly what you need it to do. then save it as a vb application and view the source code, or just use the DTS vb file for your purposes.

Sal





Similar Threads
Thread Thread Starter Forum Replies Last Post
Fixed Width text file to MSDE with C# mgudmund C# 2005 3 November 9th, 2007 05:09 AM
write text file with fixed field length hugoscp Classic ASP Professional 0 June 27th, 2007 06:12 AM
Import Fixed length - SQL SERVER 2000 carumuga SQL Server 2000 4 December 23rd, 2006 10:18 AM
DTS Fixed Width File Import rochak SQL Server DTS 3 September 26th, 2005 09:12 AM
Help me on Fixed Field Length data files hossrad SQL Server DTS 3 January 29th, 2005 07:50 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.