-VB 6.0 Professional
-Microsoft DAO 3.6 Reference
Converting Access Data to TEXT file...
An attempt to convert data from Access database to .txt file:
(1) This program attempts to transform Access data to TEXT file
(2) Use Existing database table as in the above code
(3) Each field must correspond with the field names shown here in VB
(4) You will need a fancy command button called whatever you want, 'Seek'
(5) Add Microsoft DAO 3.6 Object Library
(6) Add code below...
-
-
Private Sub CreateTXTfile_Click()
-
'dim as database to tell vb we're using an Access database
-
Dim my_database As Database
-
-
'dim as Recordsetto tell vb we're using an Access database Recordset
-
Dim objRS As Recordset
-
'add FreeFile so vb creates one locally
-
Dim intFreeFile
-
-
'open the database to grab data
-
Set my_database = OpenDatabase("C:\DataGram\Data_Central.mdb")
-
-
'open the Recordset to grab rows in a query
-
Set objRS = my_database.OpenRecordset("SELECT Your_Price, Name, Type, Crime_Rate_1, Crime_Rate_2 From LIBRARY")
-
'initiating FreeFile...
-
intFreeFile = FreeFile
-
Open App.Path + "\App_Price.txt" For Output As #intFreeFile
-
-
'Print column names for readibility
-
Print #intFreeFile, "Your_Price" & vbTab & "Name" & vbTab & "Type" & vbTab & "Crime_Rate_1" & vbTab & "Crime_Rate_2"
-
-
'run through database recordset until all data gathered to create TEXT file...
-
Do While Not objRS.EOF
-
-
Print #intFreeFile, objRS.fields("Your_Price") & vbTab _
-
& objRS.fields("Name") & vbTab & objRS.fields("Type") & vbTab _
-
& objRS.fields("Crime_Rate_1") & vbTab _
-
& objRS.fields("Crime_Rate_2")
-
-
objRS.MoveNext
-
Loop
-
-
'remember to do this...
-
objRS.Close
-
'IMPORTANT to do this to allow database to properly close
-
Set objRS = Nothing
-
-
'primitive way of clearing textboxes, you can do better here
-
'do it with a for loop
-
Text1(0).Text = ""
-
Text1(1).Text = ""
-
Text1(2).Text = ""
-
Text1(3).Text = ""
-
Text1(4).Text = ""
-
-
-
'return to first textbox to continue searching, or other
-
Text1(0).SetFocus
-
-
MsgBox ("What do you know, you have Text file(s)!")
-
-
'close the file after you create it...
-
Close intFreeFile
-
-
End Sub
-
-
Load data newly created file...
This is a little different. It's necessary to add in a textbox to record number of values to return from the multidimentional array:
(1) A record counter is needed to keep instances program loads per user
(2) An input box is also needed to record instances of your entries
-
-
Private Sub LoadTFile_Click()
-
'this collection button serves to redeeming all data written to file
-
'to be inputted back into program, dimensioning data and renaming them as "Info"
-
'Info1,2,3 and so on represent io array 1,2,3 so on and so forth
-
Dim my_string As String
-
Dim Info1 As String
-
Dim Info2 As String
-
Dim Info3 As String
-
Dim Info4 As String
-
Dim Info5 As String
-
'Dim Info6 As String
-
'Dim Info7 As String
-
'Dim Info8 As String
-
-
Dim record_cntr, location_cntr As Integer
-
Dim user_req As Integer
-
Dim bomb
-
Dim test_string As String
-
Dim X As Integer 'x is a counter
-
Dim my_char As String
-
-
-
Text1(0).Text = Info1
-
Text1(1).Text = Info2
-
Text1(2).Text = Info3
-
Text1(3).Text = Info4
-
Text1(4).Text = Info5
-
'Text1(5).Text = Info1
-
'Text1(6).Text = Info1
-
'Text1(7).Text = Info1
-
-
-
-
-
test_string = Text3.Text
-
'test_l = Len(test_string)
-
Do While X < 10
-
my_char = InStr(X, test_string)
-
Select Case my_char
-
Case "1"
-
Case "2"
-
Case "3"
-
Case "4"
-
Case "5"
-
Case "6"
-
Case "7"
-
Case "8"
-
Case "9"
-
Case "0"
-
Case Else
-
MsgBox ("You must enter a number!")
-
bomb = 99999
-
End Select
-
X = X + 1
-
Loop
-
-
If IsNumeric(Text3.Text) = False Then
-
MsgBox ("Please add numeric data to continue...")
-
'LoadTFile.Visible = False
-
Else 'If IsNumeric(Text3.Text) = True Then
-
-
-
If (bomb <> 99999) Then
-
-
user_req = Int(Text3.Text)
-
record_cntr = 1
-
-
-
filenum1 = FreeFile
-
Open App.Path + "\App_Price.txt" For Input As #filenum1 'file is opened as input because it is putting back into the program
-
-
-
Do While Not EOF(filenum1) 'this do while will work until the end of the file...otherwise it will keep going
-
Input #filenum1, Info1, Info2, Info3, Info4, Info5 ', Info6, Info7, Info8
-
record_cntr = record_cntr + 1
-
Loop
-
Close filenum1
-
-
-
If record_cntr < user_req Then
-
MsgBox ("There are only " & (record_cntr - 1) & " records in file, we will show you all records.")
-
End If
-
-
Open App.Path + "\App_Price.txt" For Input As #filenum1 'file is opened as input because it is putting back into the program
-
-
location_cntr = 1
-
Do While Not EOF(filenum1) 'this do while will work until the end of the file...otherwise it will keep going
-
Input #filenum1, Info1, Info2, Info3, Info4, Info5 ', Info6, Info7, Info8
-
'all info in textbox bellow will come through line by line using "my_string.....+vbcrlf"
-
If (location_cntr >= (record_cntr - user_req)) Then
-
my_string = my_string + Info1 + vbCrLf + Info2 + vbCrLf + Info3 + vbCrLf + Info4 + vbCrLf + Info5 + vbCrLf
-
End If
-
location_cntr = location_cntr + 1
-
Loop
-
-
Close filenum1
-
Text2.Text = my_string 'this text box return all data which have been recalled from file on harddrive or disk
-
-
-
If (prs_calc = ndvdl) Then 'this logic will take away button add if limit has been reached
-
LoadTFile.Visible = False
-
End If
-
End If
-
End If
-
End Sub
-
-
Added info:
-A user counter is also needed with the existing record_cntr
-The record_cntr along with user_req counter allow search by specific number
-A location_cntr is added to pinpoint the exact location of newly added entries
-The location_cntr find the position of the data and record counter loads number
-Textbox mentioned previously will hold number of entries needed to return
Example: if 1 is added in the textbox one row in the multidimensional array is
returned. 2, 3 and so forth would have likely results
Note: Admin or power user pages, using ASP.NET code, were created to load data to an SQL Server Database. This data will now be loaded to a user-defined website for browsing ability. Data management through VB/VBA allows XML/XSL and/or text data to load to below HTML/JavaScript driven website:
http://bytes.com/forum/thread672308.html