Connecting Tech Pros Worldwide Forums | Help | Site Map

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

Dököll's Avatar
Moderator
 
Join Date: Nov 2006
Location: Upstate NY - US
Posts: 2,268
#1   Mar 1 '08
-VB 6.0 Professional
-Microsoft DAO 3.6 Reference

Add to Database table...

An attempt to submit data to Access database:

(1) This program attempts to add, delete, update data to the Access database
(2) Build an Access database table with 5 text fields
(3) Read code below, add these fields starting with Your_Price as names
(4) Each field must correspond with the field names shown here in VB
(5) You must have an array of 5 textboxes, first in line Text1(0).Text
(6) You will need a fancy command button called whatever you want, 'Seek'
(7) ADD Microsoft DAO 3.6 Object Library
(8) Add code below...

Expand|Select|Wrap|Line Numbers
  1.  
  2. Private Sub subt_Click()   'this function will load entry into database
  3. 'dim as database to allow vb to interact with Access database seemlessly...
  4.         Dim my_database As Database
  5. 'open database to allow vb to add data to Access database seemlessly...
  6.         Set my_database = OpenDatabase("C:\DataGram\Data_Central.mdb")
  7. 'run insert statement query that will load data to your database
  8.         my_database.Execute "insert into Data_Central.LIBRARY(Your_Price, Name, Type, Crime_Rate_1, Crime_Rate_2) Values('" & Text1(0).Text & "','" & Text1(1).Text & "' , '" & Text1(2).Text & "' , '" & Text1(3).Text & "','" & Text1(4).Text & "')"
  9.         my_database.Close
  10. 'this variable serves to emptying your textboxes, part of reset button
  11.     R_Click
  12.  
  13. End Sub
  14.  
  15.  
Delete Data from database...

Expand|Select|Wrap|Line Numbers
  1.  
  2. Private Sub dll_Click()
  3. 'dim as database to allow vb to interact with Access database seemlessly...
  4. Dim my_database As Database
  5. 'dim as Recordset to allow vb to interact with Access database seemlessly...
  6. Dim my_record As Recordset
  7. 'open database to allow vb to delete data from Access database seemlessly...
  8. Set my_database = OpenDatabase("C:\DataGram\Data_Central.mdb")
  9. 'run delete statement query that will remove data from your database
  10. Set my_record = my_database.OpenRecordset("select * from LIBRARY where Your_Price='" & Text1(0).Text & "'")
  11. If Not my_record.EOF Then
  12. my_record.Delete
  13. End If
  14. my_database.Close
  15. 'variable that should empty textboxes for future instances...
  16. R_Click
  17.  
  18. End Sub
  19.  
  20.  
Update Data in database...

Expand|Select|Wrap|Line Numbers
  1.  
  2. Private Sub Updt_Click()
  3. 'dim as database to allow vb to interact with Access database seemlessly...
  4. Dim my_database As Database
  5. 'dim as Recordset to allow vb to interact with Access database seemlessly...
  6. Dim my_record As Recordset
  7. 'run a little check to see if proper credentials are added before releasing info...
  8. If (Text1(0) = "") Then
  9. MsgBox ("Please put in Your price..."), vbOKOnly, "Data Mining Error"
  10. ElseIf (Text1(1) = "") Then
  11. MsgBox ("Please put in Item name..."), vbOKOnly, "Data Mining Error"
  12. Else
  13. 'open database to allow vb to update data to Access database seemlessly...
  14. Set my_database = OpenDatabase("C:\DataGram\Data_Central.mdb")
  15. 'run update statement query that will modify data to your database
  16. Set my_record = my_database.OpenRecordset("SELECT * FROM LIBRARY WHERE Your_Price='" & Text1(0).Text & "'")
  17. my_record.Edit
  18.                 my_record!Your_Price = Text1(0).Text
  19.                 my_record!Name = Text1(1).Text
  20.                 my_record!Type = Text1(2).Text
  21.                 my_record!Crime_Rate_1 = Text1(3).Text
  22.                 my_record!Crime_Rate_2 = Text1(4).Text
  23.  
  24. my_record.Update
  25. my_record.Close
  26. my_database.Close
  27.  
  28. End If
  29.  
  30. End Sub
  31.  
  32.  
Clear your textboxes...

Expand|Select|Wrap|Line Numbers
  1. Private Sub R_Click() 'this must clear the textbox upon entry of all data
  2.         Text1(0).Text = ""      
  3.         Text1(1).Text = ""
  4.         Text1(2).Text = ""
  5.         Text1(3).Text = ""
  6.         Text1(4).Text = ""
  7.  
  8. Text1(0).SetFocus       'this should set index back to first textbox
  9. End Sub
  10.  
Convert Data to XML file next...


-Add Microsoft XML v 3.0
-Add code below

Contiued from:

http://www.thescripts.com/forum/thread777267.html

-VB 6.0 Professional
-Microsoft XML v3.0 Reference

Converting Access Data to XML file...

An attempt to convert data from Access database to XML:

(1) This program attempts to transform Access data to XML
(2) Use Existing database table, load above code
(4) Each field must correspond with the field names shown here in VB
(5) You will need a fancy command button called whatever you want, 'Seek'
(6) ADD Microsoft XML v3.0 Object Library, in References
(7) Add Microsoft DAO 3.6 Object Library
(8) Add code below...

Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3. Private Sub CreateXMFile_Click()
  4. 'dim as database to allow vb to interact with Access database seemlessly...
  5. Dim my_database As Database
  6. 'dim as Recordset to allow vb to interact with Access database seemlessly...
  7. Dim objRS As Recordset
  8.     dimension your FreeFile
  9.     Dim intFreeFile
  10. 'open database to allow vb to grab data from Access database seemlessly...    
  11.     Set my_database = OpenDatabase("C:\DataGram\Data_Central.mdb")
  12. 'run query that will collect data from your database
  13.     Set objRS = my_database.OpenRecordset("SELECT Your_Price, Name, Type, Crime_Rate_1, Crime_Rate_2 From LIBRARY")
  14. intFreeFile = FreeFile
  15. Open App.Path + "\App_Price.xml" For Output As #intFreeFile
  16.  
  17. 'build XML version number, and print column names for readibility...
  18. Print #intFreeFile, "<?xml version=" & Chr(34) & "1.0" & Chr(34) & "?>"
  19. Print #intFreeFile, "<!--  File Name: App_Price.xml -->"
  20.  
  21. 'printing out nodes and subnodes with data gathered from database...
  22. Print #intFreeFile, "<Find_It>"
  23.  
  24.     Do While Not objRS.EOF
  25.     Print #intFreeFile, "<Apartmnt>"
  26.         Print #intFreeFile, "<Your_Price>" & objRS.fields("Your_Price") & "</Your_Price>" _
  27.         & vbCrLf & "<Name>" & objRS.fields("Name") & "</Name>" & vbCrLf & "<Type>" & objRS.fields("Type") & "</Type>" _
  28.         & vbCrLf & "<Crime_Rate_1>" & objRS.fields("Crime_Rate_1") & "</Crime_Rate_1>" _
  29.         & vbCrLf & "<Crime_Rate_2>" & objRS.fields("Crime_Rate_2") & "</Crime_Rate_2>"
  30.     Print #intFreeFile, "</Apartmnt>"
  31.         objRS.MoveNext
  32.     Loop
  33.  
  34. Print #intFreeFile, "</Find_It>"
  35.  
  36.     objRS.Close
  37.     Set objRS = Nothing
  38.  Close intFreeFile
  39.     MsgBox ("What do you know, you have an XML file!")
  40. 'depending on number of instances added in input box upon entry, 
  41. 'you will have x amount of instances to enter, browse data through this application.  Button disappears after your max has been reached:-)  
  42.     If (prs_calc = ndvdl) Then     'this logic will take away button CreateXMFile if limit has been reached
  43.             CreateXMFile.Visible = False
  44.         End If
  45. End Sub
  46.  
  47.  
See next: http://bytes.com/forum/thread777278.html

Last edited by Dököll; Apr 13 '08 at 03:55 AM. Reason: title modified + comments...



Reply


Similar Visual Basic 4 / 5 / 6 bytes