Simple I know but I cannot understand why this code gives me grief.
I get a type mismatch on the green line.
rsecordset is open
.eof=false
if in debug I hover over nfield it = nothing
cannot understand it please help.
Public Sub dbtocsv()
'************************************************************
'variables for writing files
'************************************************************
Dim filesystemobject, DocsStream As Object, i As Long
Dim d As String
Dim Nfield As Field
Dim rs As ADODB.Recordset
Dim sqltext As String
Dim nfile As Integer
Dim fname As String
'***********************************************************
'Procedure:
'**********************************************************
'open database
OpenDBShared db_path_name
'create new Recordset
Set rs = New ADODB.Recordset
rs.ActiveConnection = m_DBConnection.ConnectionString
gsStart 150, frmbptocsv.hwnd
For nfile = 0 To 9
'handling the file selection to create
' by checking if the sqltext is not blank
If query(nfile) <> "" Then
sqltext = query(nfile)'comment(#query(nfile="Select*From BPFARROW)
fname = Add_Slash(App.path) & filename(nfile) 'filepath
rs.Open sqltext
If rs.BOF And rs.EOF Then 'handling the empty recordsets
exit sub
Else
rs.MoveFirst
End If
'Create a file object for the output file
Set filesystemobject = CreateObject("Scripting.Filesystemobject"
Set DocsStream = filesystemobject.CreateTextFile(fname, True)
Do Until rs.EOF
d = ""
'Loop through the fields in the database
For Each Nfield In rs.Fields()
'If First time no comma needed, otherwise put a comma before the data
If d <> "" Then
d = d + ","
End If
'Convert the database field to text depending on type and place it after the comma
If Not IsNull(Nfield) Then
If 7 = Nfield.Type Then
If Nfield > 1 Then
'a date
d = d + Format(Nfield, "dd/mm/yyyy"
Else
'a time
d = d + Format(Nfield, "hh:mm"
End If
ElseIf 5 = Nfield.Type Or 2 = Nfield.Type Or 4 = Nfield.Type Then
'Numeric
d = d + str(Nfield)
ElseIf 11 = Nfield.Type Then
'Boolean
If Nfield Then
d = d + "True"
Else
d = d + "False"
End If
Else
d = d + Nfield
End If
End If
end if
Next Nfield
'Write the record to the file
DocsStream.WriteLine (d)
rs.MoveNext
Loop
DocsStream.Close
End If
rs.Close
gsTick
Next nfile
gsStop
Unload Me
end sub
:
I get a type mismatch on the green line.
rsecordset is open
.eof=false
if in debug I hover over nfield it = nothing
cannot understand it please help.
Public Sub dbtocsv()
'************************************************************
'variables for writing files
'************************************************************
Dim filesystemobject, DocsStream As Object, i As Long
Dim d As String
Dim Nfield As Field
Dim rs As ADODB.Recordset
Dim sqltext As String
Dim nfile As Integer
Dim fname As String
'***********************************************************
'Procedure:
'**********************************************************
'open database
OpenDBShared db_path_name
'create new Recordset
Set rs = New ADODB.Recordset
rs.ActiveConnection = m_DBConnection.ConnectionString
gsStart 150, frmbptocsv.hwnd
For nfile = 0 To 9
'handling the file selection to create
' by checking if the sqltext is not blank
If query(nfile) <> "" Then
sqltext = query(nfile)'comment(#query(nfile="Select*From BPFARROW)
fname = Add_Slash(App.path) & filename(nfile) 'filepath
rs.Open sqltext
If rs.BOF And rs.EOF Then 'handling the empty recordsets
exit sub
Else
rs.MoveFirst
End If
'Create a file object for the output file
Set filesystemobject = CreateObject("Scripting.Filesystemobject"
Set DocsStream = filesystemobject.CreateTextFile(fname, True)
Do Until rs.EOF
d = ""
'Loop through the fields in the database
For Each Nfield In rs.Fields()
'If First time no comma needed, otherwise put a comma before the data
If d <> "" Then
d = d + ","
End If
'Convert the database field to text depending on type and place it after the comma
If Not IsNull(Nfield) Then
If 7 = Nfield.Type Then
If Nfield > 1 Then
'a date
d = d + Format(Nfield, "dd/mm/yyyy"
Else
'a time
d = d + Format(Nfield, "hh:mm"
End If
ElseIf 5 = Nfield.Type Or 2 = Nfield.Type Or 4 = Nfield.Type Then
'Numeric
d = d + str(Nfield)
ElseIf 11 = Nfield.Type Then
'Boolean
If Nfield Then
d = d + "True"
Else
d = d + "False"
End If
Else
d = d + Nfield
End If
End If
end if
Next Nfield
'Write the record to the file
DocsStream.WriteLine (d)
rs.MoveNext
Loop
DocsStream.Close
End If
rs.Close
gsTick
Next nfile
gsStop
Unload Me
end sub
: