VB icon

transferring data

Submitted on: 1/23/2015 2:07:00 AM
By: Syrob (from psc cd)  
Level: Intermediate
User Rating: By 4 Users
Compatibility: VB 6.0
Views: 473
     This VB code shows how you can use ADO and Microsoft Scripting Runtime Library to transfer data from a database table to a text file.
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
' Name: transferring data
' Description:This VB code shows how you can use ADO and Microsoft Scripting Runtime Library to transfer data from a database table to a text file.
' By: Syrob (from psc cd)
' Assumes:This code uses the Microsoft Scripting Runtime Library and Microsoft ActiveX Data Objects Library, so to use it make sure you check off the Microsoft Scripting Runtime and ADO in Visual Basics References.

Private Sub Command1_Click()
' declare a constant
Const conType As Integer = 8209
'declare an object variable
Dim rs As ADODB.Recordset
' create a recordset object and
' assign it to the object variable
Set rs = New ADODB.Recordset
' execute the open method
With rs
.ActiveConnection = _
"provider=microsoft.jet.oledb.4.0;" _
& "data source = c:yourdb.mdb"
 .Source = "select * from yourtable"
 End With
' declare a dynamic array
Dim str()
' execute the getrows method
str() = rs.GetRows
' we do not need the recordset object
Set rs = Nothing
' declare the object variables
Dim fso As FileSystemObject
Dim txtfile As TextStream
' create a filesystemobject
Set fso = _
' execute the createtextfile function
Set txtfile = _
fso.CreateTextFile("c:\testfile.txt", True)
' we do not need the object
Set fso = Nothing
' declare the variables that
' will hold the values from the array
Dim v, strv
' declare the variables
Dim j As Integer
Dim x As Integer
' loop through the array
For x = LBound(str, 2) To UBound(str, 2)
 For j = LBound(str, 1) To UBound(str, 1)
' dump a value from the array into
' the strv variable and do
' with it whatever you want
 strv = str(j, x)
' it is an example what you can do
 If VarType(strv) = conType Then
 strv = CStr(strv)
 strv = "Picture"
 ElseIf IsNull(strv) Then
 strv = "Null"
 ElseIf strv = "" Then
 strv = """"
 ElseIf j > 0 Then
 strv = "'" & strv & "'"
 End If
' build a text line
 v = v & strv & ", "
' check if the loop reached the end of a row
 If j = UBound(str, 1) Then
' since the whole record was dumped into
' the v variable, the text line can
' be written into the file
 txtfile.WriteLine v
' reset the variable
 v = ""
 End If
 Next j
 Next x
' close the object
End Sub

Other 2 submission(s) by this author


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Intermediate category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)

Other User Comments

 There are no comments on this submission.

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)

To post feedback, first please login.