Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

recordset open help required 1

Status
Not open for further replies.

sidharthrshah

Programmer
Jun 21, 2003
5
0
0
IN
I have the code for converting an access database to comma delimited file.But it requires recordset that has to be exported and I have to open the recordset before passing it to this function.So how do I do this?

I want all the fields of the table sidtable in recordset.
the table fields are : nam,telno,addr
the access file name is collproj.mdb

Please help.
------------------------------------------------------------

'PURPOSE: EXPORTS DATA FROM AN ADO RECORDSET TO A TEXT FILE

'PARAMETERS:

'RS: Recordset to Export. Open the recordset before
'passing it to this function

'FullPath (Optional): FullPath of text file.
'if not specified, the function uses app.path +
'rs.txt

'ValueDelmiter (Optional): String to delimiter
'values within a row. If not specified, an empty space
'is used

'RETURNS: True if successful, false if an error occurs
-----------------------------------------------------
Public Function RecordsetToText(rs As Object, Optional FullPath _
As String, Optional ValueDelimiter As String = " ") As Boolean

'PURPOSE: EXPORTS DATA FROM AN ADO RECORDSET TO A TEXT FILE

'PARAMETERS:

'RS: Recordset to Export. Open the recordset before
'passing it to this function

'FullPath (Optional): FullPath of text file.
'if not specified, the function uses app.path +
'rs.txt

'ValueDelmiter (Optional): String to delimiter
'values within a row. If not specified, an empty space
'is used

'RETURNS: True if successful, false if an error occurs

'COMMENTS: Rows are delimited by a carriage return
Dim sFullPath As String
Dim sDelimiter As String
Dim iFileNum As Integer
Dim lFieldCount As Long
Dim lCtr As Long
Dim oField As ADODB.Field


On Error GoTo ErrorHandler:


If RecordSetReady(rs) = False Then Exit Function

MsgBox ("the function has sarted")
sDelimiter = ValueDelimiter

If FullPath = "" Then
sFullPath = App.Path
If Right(sFullPath, 1) <> &quot;\&quot; Then sFullPath = _
sFullPath & &quot;\&quot;

sFullPath = sFullPath & &quot;rs.txt&quot;
Else
sFullPath = FullPath

End If

iFileNum = FreeFile

Open sFullPath For Output As #iFileNum

With rs
lFieldCount = .Fields.Count - 1

On Error Resume Next
.MoveFirst
On Error GoTo ErrorHandler

Do While Not .EOF

For lCtr = 0 To lFieldCount

Set oField = .Fields(lCtr)
If FieldCanBeString(oField) Then
If lCtr < lFieldCount Then
Print #iFileNum, oField.Name & &quot;:&quot; & _
oField.Value & sDelimiter;

Else
Print #iFileNum, oField.Name & &quot;: &quot; & oField.Value
End If

End If
Next

.MoveNext
Loop

End With

RecordsetToText = True

ErrorHandler:
On Error Resume Next
Close #iFileNum

End Function

Private Function RecordSetReady(rs As Object) As Boolean
'Recordset must be opened and connected

On Error Resume Next

If rs Is Nothing Then Exit Function
If Not TypeOf rs Is ADODB.Recordset Then Exit Function

If rs.State = 0 Then
'attempt to open, requires source has populated and either
'source or recordset has an active connection
On Error Resume Next
rs.open
If Err.Number <> 0 Then Exit Function
End If

RecordSetReady = True

End Function

Private Function FieldCanBeString(oField) _
As Boolean

If IsObject(FieldObj.Value) Then
FieldCanBeString = False
Else
'Assumes adEmpty will be converted to &quot;&quot;

'Doesn't check for null value because
'Assumes null will be converted to &quot;&quot;
Select Case FieldObj.Type
Case adBinary, adIDispatch, adIUnknown, adUserDefined
FieldCanBeString = False
Case Else
FieldCanBeString = True
End Select
End If

End Function

'Private Function RecordSetReady1(rs As Object) As Boolean

'End Function

 
why not just use the DoCmd.TransferText action, all it requires is for you to create and save an Export Specification that can be called as part of the command line. You can export to a text file in either Fixed Length or as a Delimited file.

PaulF
 
Can you please give me some information in detail as I am totally unaware of this.Thanks for your help.

Sidharth
 
select your table or query in the database window, right-click on the mouse, select &quot;Export&quot;. You should see the Save Dialog box open, select &quot;Text File&quot; from the &quot;Save As Type&quot; dropdown box. Select the desired folder to save the file to, and enter the filename you want, then click on the &quot;Export&quot; command button. The Export Wizard should appear, select Delimited, then click on the &quot;Advanced...&quot; button, and make any changes you need, then click on the &quot;Save As&quot; button, and enter the a name for this specification, then click &quot;OK&quot; which closes the &quot;Save As&quot; dialog box, and click &quot;OK&quot; again to exit the Advanced options.. continue to click &quot;Next&quot; til you get to the &quot;Finish&quot; button, and you can click this and check out the results. After you have saved this Export Specification you can call it as part of the DoCmd.TransferText action.

DoCmd.TransferText acExportDelim, &quot;Specification Name&quot;, &quot;Table or Query Name&quot;, &quot;Path and File Name&quot;, False, &quot;&quot;

the False is for &quot;Has Field Names&quot; which would be set to TRUE if you wanted the first line to include your field names, the last part deals with HTML table name, so you don't have to put anything in it. You can use a macro to enter this data if you want to, and then call the macro.

Hope this helps

PaulF
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top