Connecting Tech Pros Worldwide Help | Site Map

Data management with XML, Text files using VB 6.0: Part Three

Dököll's Avatar
Moderator
 
Join Date: Nov 2006
Location: Upstate NY - US
Posts: 2,267
#1   Mar 1 '08
-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...

Expand|Select|Wrap|Line Numbers
  1.  
  2. Private Sub CreateTXTfile_Click()
  3. 'dim as database to tell vb we're using an Access database
  4. Dim my_database As Database
  5.  
  6. 'dim as Recordsetto tell vb we're using an Access database Recordset
  7. Dim objRS As Recordset
  8. 'add FreeFile so vb creates one locally
  9.     Dim intFreeFile
  10.  
  11. 'open the database to grab data
  12.     Set my_database = OpenDatabase("C:\DataGram\Data_Central.mdb")
  13.  
  14. 'open the Recordset to grab rows in a query
  15.     Set objRS = my_database.OpenRecordset("SELECT Your_Price, Name, Type, Crime_Rate_1, Crime_Rate_2 From LIBRARY")
  16. 'initiating FreeFile...
  17.     intFreeFile = FreeFile
  18. Open App.Path + "\App_Price.txt" For Output As #intFreeFile
  19.  
  20. 'Print column names for readibility
  21. Print #intFreeFile, "Your_Price" & vbTab & "Name" & vbTab & "Type" & vbTab & "Crime_Rate_1" & vbTab & "Crime_Rate_2"
  22.  
  23. 'run through database recordset until all data gathered to create TEXT file...   
  24.    Do While Not objRS.EOF
  25.  
  26.         Print #intFreeFile, objRS.fields("Your_Price") & vbTab _
  27.         & objRS.fields("Name") & vbTab & objRS.fields("Type") & vbTab _
  28.         & objRS.fields("Crime_Rate_1") & vbTab _
  29.         & objRS.fields("Crime_Rate_2")
  30.  
  31.         objRS.MoveNext
  32.     Loop
  33.  
  34.     'remember to do this...
  35.     objRS.Close
  36.     'IMPORTANT to do this to allow database to properly close
  37.     Set objRS = Nothing
  38.  
  39.         'primitive way of clearing textboxes, you can do better here
  40.         'do it with a for loop
  41.         Text1(0).Text = ""
  42.         Text1(1).Text = ""
  43.         Text1(2).Text = ""
  44.         Text1(3).Text = ""
  45.         Text1(4).Text = ""
  46.  
  47.  
  48.         'return to first textbox to continue searching, or other
  49.         Text1(0).SetFocus
  50.  
  51.         MsgBox ("What do you know, you have Text file(s)!")
  52.  
  53. 'close the file after you create it...    
  54. Close intFreeFile
  55.  
  56. End Sub
  57.  
  58.  
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

Expand|Select|Wrap|Line Numbers
  1.  
  2. Private Sub LoadTFile_Click()
  3.       'this collection button serves to redeeming all data written to file
  4.       'to be inputted back into program, dimensioning data and renaming them as "Info"
  5.       'Info1,2,3 and so on represent io array 1,2,3 so on and so forth
  6.     Dim my_string As String
  7.     Dim Info1 As String
  8.     Dim Info2 As String
  9.     Dim Info3 As String
  10.     Dim Info4 As String
  11.     Dim Info5 As String
  12.     'Dim Info6 As String
  13.     'Dim Info7 As String
  14.     'Dim Info8 As String
  15.  
  16.     Dim record_cntr, location_cntr As Integer
  17.     Dim user_req As Integer
  18.     Dim bomb
  19.     Dim test_string As String
  20.     Dim X As Integer 'x is a counter
  21.     Dim my_char As String
  22.  
  23.  
  24.     Text1(0).Text = Info1
  25.     Text1(1).Text = Info2
  26.     Text1(2).Text = Info3
  27.     Text1(3).Text = Info4
  28.     Text1(4).Text = Info5
  29.     'Text1(5).Text = Info1
  30.     'Text1(6).Text = Info1
  31.     'Text1(7).Text = Info1
  32.  
  33.  
  34.  
  35.  
  36.     test_string = Text3.Text
  37.     'test_l = Len(test_string)
  38.     Do While X < 10
  39.         my_char = InStr(X, test_string)
  40.         Select Case my_char
  41.         Case "1"
  42.         Case "2"
  43.         Case "3"
  44.         Case "4"
  45.         Case "5"
  46.         Case "6"
  47.         Case "7"
  48.         Case "8"
  49.         Case "9"
  50.         Case "0"
  51.         Case Else
  52.             MsgBox ("You must enter a number!")
  53.             bomb = 99999
  54.         End Select
  55.         X = X + 1
  56.     Loop
  57.  
  58.     If IsNumeric(Text3.Text) = False Then
  59.     MsgBox ("Please add numeric data to continue...")
  60.     'LoadTFile.Visible = False
  61.     Else 'If IsNumeric(Text3.Text) = True Then
  62.  
  63.  
  64.     If (bomb <> 99999) Then
  65.  
  66.                 user_req = Int(Text3.Text)
  67.     record_cntr = 1
  68.  
  69.  
  70.                     filenum1 = FreeFile
  71.                 Open App.Path + "\App_Price.txt" For Input As #filenum1        'file is opened as input because it is putting back into the program
  72.  
  73.  
  74.                     Do While Not EOF(filenum1)                                    'this do while will work until the end of the file...otherwise it will keep going
  75.                             Input #filenum1, Info1, Info2, Info3, Info4, Info5 ', Info6, Info7, Info8
  76.                             record_cntr = record_cntr + 1
  77.                     Loop
  78.                     Close filenum1
  79.  
  80.  
  81.                     If record_cntr < user_req Then
  82.                         MsgBox ("There are only " & (record_cntr - 1) & " records in file, we will show you all records.")
  83.                     End If
  84.  
  85.                 Open App.Path + "\App_Price.txt" For Input As #filenum1        'file is opened as input because it is putting back into the program
  86.  
  87.                 location_cntr = 1
  88.                     Do While Not EOF(filenum1)                                    'this do while will work until the end of the file...otherwise it will keep going
  89.                         Input #filenum1, Info1, Info2, Info3, Info4, Info5 ', Info6, Info7, Info8
  90.                         'all info in textbox bellow will come through line by line using "my_string.....+vbcrlf"
  91.                             If (location_cntr >= (record_cntr - user_req)) Then
  92.                                     my_string = my_string + Info1 + vbCrLf + Info2 + vbCrLf + Info3 + vbCrLf + Info4 + vbCrLf + Info5 + vbCrLf
  93.                             End If
  94.                             location_cntr = location_cntr + 1
  95.                     Loop
  96.  
  97.                 Close filenum1
  98.                         Text2.Text = my_string    'this text box return all data which have been recalled from file on harddrive or disk
  99.  
  100.  
  101. If (prs_calc = ndvdl) Then     'this logic will take away button add if limit has been reached
  102.             LoadTFile.Visible = False
  103.         End If
  104. End If
  105. End If
  106. End Sub
  107.  
  108.  

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

Last edited by Dököll; Apr 13 '08 at 03:50 AM. Reason: comment...



Reply


Similar Visual Basic 4 / 5 / 6 bytes