dbTalk Databases Forums  

dbf->csv

comp.databases.xbase.fox comp.databases.xbase.fox


Discuss dbf->csv in the comp.databases.xbase.fox forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Walid Khalid
 
Posts: n/a

Default dbf->csv - 09-07-2004 , 05:56 PM






This vb code to convert database dbf file to asccii csv format
you can open the file then as text file. I am saving the file
to "c:\" as table.txt.
you need:
1.create form
2.add common dialoge
3.add command button
4. copy and paste code

hope someone can develope it to some thing better.

Walid
New Zealand

';; ! ************************************************** *************************
';; ! dbf to csv
';; ! ************************************************** *************************
';; ! Function : Create csv file from dbf file
';; ! Updated : Sept 08, 2004
';; ! (C) 1999-2004, Geographic Information System, Rodney District
Council
';; ! Contact :Walid Ismail Al Tiay
';; !
';; !
';; ! ************************************************** *************************

Option Explicit

Dim fdesc As FdescriptorArray

Private Sub Command1_Click()
Dim sfile As String
Dim sfile2 As String

With CommonDialog1
.DialogTitle = "Open"
.CancelError = False
.FileName = ""
.Filter = "DBase III Files (*.DBF)|*.DBF|All Files (*.*)|*.*"
.ShowOpen
If Len(.FileName) = 0 Then
Exit Sub
End If
sfile2 = .FileTitle
sfile = .FileName
End With

Open "c:\table.txt" For Output As #1
ParseDBase sfile, sfile2
End Sub

Function ParseDBase(p_strFileName As String, p_strFileName2 As String)
As String
Dim Version As Byte
Dim intFF As Integer
Dim lngNumRec As Long
Dim intHLen As Integer
Dim intRLen As Integer
Dim strLASTUPD As String * 3
Dim bytIncTrans As Byte
Dim bytEncFlag As Byte
Dim DbaseContainer As String * 263
Dim pointer As Long
Dim record() As Byte
Dim FieldNames() As FdescriptorArray
Dim strTemp As String
Dim I As Integer
Dim lngCounter As Long
Dim msg As String

intFF = FreeFile
Open (p_strFileName) For Binary Access Read As #intFF
Get #intFF, 1, Version 'DBase Version
Get #intFF, 2, strLASTUPD
Get #intFF, 5, lngNumRec 'Records
Get #intFF, 9, intHLen
Get #intFF, 11, intRLen
Get #intFF, 33, fdesc

ReDim record(1 To intRLen) As Byte
ReDim FieldNames(0 To 0)

pointer = 33 + Len(fdesc)

Do Until pointer >= intHLen
Get #intFF, pointer, fdesc
ReDim Preserve FieldNames(0 To UBound(FieldNames) + 1)
FieldNames(UBound(FieldNames) - 1) = fdesc
pointer = pointer + Len(fdesc)
Loop

lngCounter = 1

Do Until EOF(intFF)
strTemp = ""
Get #intFF, , record

ParseRecord FieldNames, record, lngCounter
lngCounter = lngCounter + 1
Loop


Close #intFF
Close #1
MsgBox "Done....."

End Function

Private Sub ParseRecord(FieldNames() As FdescriptorArray,
p_bytRecord() As Byte, p_lngCounter As Long)
Dim J As Integer
Dim I As Integer
Dim position As Integer
Dim strTemp As String
Dim msg As String
Dim lngFieldLen As Long

On Error GoTo Errorhandler

position = 0
For I = 0 To UBound(FieldNames)
lngFieldLen = FieldNames(I).FieldLength + 15
For J = 1 To lngFieldLen
On Error Resume Next
strTemp = strTemp & Chr$(p_bytRecord(J + position))
Next

If I <> UBound(FieldNames) Then
msg = msg & "," & Trim$(strTemp)
Else
Print #1, msg & "," & Trim$(strTemp)
End If

position = position + lngFieldLen
strTemp = ""
Next

Errorhandler:

End Sub

Private Sub Form_Load()
Command1.Caption = "Open"
End Sub

Private Sub Form_Unload(Cancel As Integer)
End
End Sub

Reply With Quote
  #2  
Old   
swdev2
 
Posts: n/a

Default Re: dbf->csv - 09-13-2004 , 12:57 AM






Heya Walid -
why would you 'add 15' into every field/column length ?
or did I mis-read your code ?
warmest regards [Bill]
--
William Sanders / Electronic Filing Group Remove the DOT BOB to reply via
email.
Mondo Cool TeleCom -> http://www.efgroup.net/efgcog.html
Mondo Cool WebHosting -> http://www.efgroup.net/efglunar.html
Mondo Cool Satellites -> http://www.efgroup.net/sat
VFP Webhosting? You BET! -> http://efgroup.net/vfpwebhosting
mySql / VFP / MS-SQL

"Walid Khalid" <walidaltiay (AT) yahoo (DOT) com> wrote

Quote:
This vb code to convert database dbf file to asccii csv format
you can open the file then as text file. I am saving the file
to "c:\" as table.txt.
you need:
1.create form
2.add common dialoge
3.add command button
4. copy and paste code

hope someone can develope it to some thing better.

Walid
New Zealand

';; !
************************************************** *************************
';; ! dbf to csv
';; !
************************************************** *************************
';; ! Function : Create csv file from dbf file
';; ! Updated : Sept 08, 2004
';; ! (C) 1999-2004, Geographic Information System, Rodney District
Council
';; ! Contact :Walid Ismail Al Tiay
';; !
';; !
';; !
************************************************** *************************

snip
For I = 0 To UBound(FieldNames)
lngFieldLen = FieldNames(I).FieldLength + 15
For J = 1 To lngFieldLen
On Error Resume Next
strTemp = strTemp & Chr$(p_bytRecord(J + position))
Next
snip




Reply With Quote
  #3  
Old   
Walid Khalid
 
Posts: n/a

Default Re: dbf->csv - 09-16-2004 , 10:06 PM



Sorry I think I forget to add the module to the code:
Option Explicit
'++++++++++++++++++++++++++++++++++
Public Type FdescriptorArray
FieldName As String * 11
FieldType As Byte
FieldDataAddress As String * 4
FieldLength As Byte
FieldDecimalCount As Byte
Something As String * 2
WAreaID As Byte
Something2 As String * 2
Flag As Byte
Reserved As String * 7
IDXFldFlag As Byte
End Type
'+++++++++++++++++++++++++++++++++++++++



walidaltiay (AT) yahoo (DOT) com (Walid Khalid) wrote in message news:<21247a28.0409071456.4724f0c4 (AT) posting (DOT) google.com>...
Quote:
This vb code to convert database dbf file to asccii csv format
you can open the file then as text file. I am saving the file
to "c:\" as table.txt.
you need:
1.create form
2.add common dialoge
3.add command button
4. copy and paste code

hope someone can develope it to some thing better.

Walid
New Zealand

';; ! ************************************************** *************************
';; ! dbf to csv
';; ! ************************************************** *************************
';; ! Function : Create csv file from dbf file
';; ! Updated : Sept 08, 2004
';; ! (C) 1999-2004, Geographic Information System, Rodney District
Council
';; ! Contact :Walid Ismail Al Tiay
';; !
';; !
';; ! ************************************************** *************************

Option Explicit

Dim fdesc As FdescriptorArray

Private Sub Command1_Click()
Dim sfile As String
Dim sfile2 As String

With CommonDialog1
.DialogTitle = "Open"
.CancelError = False
.FileName = ""
.Filter = "DBase III Files (*.DBF)|*.DBF|All Files (*.*)|*.*"
.ShowOpen
If Len(.FileName) = 0 Then
Exit Sub
End If
sfile2 = .FileTitle
sfile = .FileName
End With

Open "c:\table.txt" For Output As #1
ParseDBase sfile, sfile2
End Sub

Function ParseDBase(p_strFileName As String, p_strFileName2 As String)
As String
Dim Version As Byte
Dim intFF As Integer
Dim lngNumRec As Long
Dim intHLen As Integer
Dim intRLen As Integer
Dim strLASTUPD As String * 3
Dim bytIncTrans As Byte
Dim bytEncFlag As Byte
Dim DbaseContainer As String * 263
Dim pointer As Long
Dim record() As Byte
Dim FieldNames() As FdescriptorArray
Dim strTemp As String
Dim I As Integer
Dim lngCounter As Long
Dim msg As String

intFF = FreeFile
Open (p_strFileName) For Binary Access Read As #intFF
Get #intFF, 1, Version 'DBase Version
Get #intFF, 2, strLASTUPD
Get #intFF, 5, lngNumRec 'Records
Get #intFF, 9, intHLen
Get #intFF, 11, intRLen
Get #intFF, 33, fdesc

ReDim record(1 To intRLen) As Byte
ReDim FieldNames(0 To 0)

pointer = 33 + Len(fdesc)

Do Until pointer >= intHLen
Get #intFF, pointer, fdesc
ReDim Preserve FieldNames(0 To UBound(FieldNames) + 1)
FieldNames(UBound(FieldNames) - 1) = fdesc
pointer = pointer + Len(fdesc)
Loop

lngCounter = 1

Do Until EOF(intFF)
strTemp = ""
Get #intFF, , record

ParseRecord FieldNames, record, lngCounter
lngCounter = lngCounter + 1
Loop


Close #intFF
Close #1
MsgBox "Done....."

End Function

Private Sub ParseRecord(FieldNames() As FdescriptorArray,
p_bytRecord() As Byte, p_lngCounter As Long)
Dim J As Integer
Dim I As Integer
Dim position As Integer
Dim strTemp As String
Dim msg As String
Dim lngFieldLen As Long

On Error GoTo Errorhandler

position = 0
For I = 0 To UBound(FieldNames)
lngFieldLen = FieldNames(I).FieldLength + 15
For J = 1 To lngFieldLen
On Error Resume Next
strTemp = strTemp & Chr$(p_bytRecord(J + position))
Next

If I <> UBound(FieldNames) Then
msg = msg & "," & Trim$(strTemp)
Else
Print #1, msg & "," & Trim$(strTemp)
End If

position = position + lngFieldLen
strTemp = ""
Next

Errorhandler:

End Sub

Private Sub Form_Load()
Command1.Caption = "Open"
End Sub

Private Sub Form_Unload(Cancel As Integer)
End
End Sub

Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.