473,394 Members | 1,766 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

What does this visual basic code do????(line by line..)

1
Expand|Select|Wrap|Line Numbers
  1. Dim db As Database
  2. Dim rs As Recordset
  3. Dim WS As Workspace
  4. Dim Max As Long
  5.  
  6.  
  7. Private Sub Form_Load()
  8. Set WS = DBEngine.Workspaces(0)
  9.     DbFile = (App.Path & "\Database\AddressBook.mdb")
  10.     PwdString = "swordfish"
  11. Set db = DBEngine.OpenDatabase(DbFile, False, False, ";PWD=" & PwdString)
  12. Set rs = db.OpenRecordset("Addresses", dbOpenTable)
  13. Max = rs.RecordCount
  14. lblTotalContacts.Caption = Max
  15. If rs.RecordCount = 0 Then
  16. Exit Sub
  17. Else
  18. rs.MoveFirst
  19.  
  20. List1.Clear
  21.  
  22. For i = 1 To Max
  23.     List1.AddItem rs!FullName
  24.     rs.MoveNext
  25. Next i
  26. List1.ListIndex = 0
  27. End If
  28. ButtonsEnabled
  29. End Sub
  30.  
  31. Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
  32. ButtonsEnabled
  33. End Sub
  34.  
  35. Private Sub Form_Unload(Cancel As Integer)
  36. End
  37. End Sub
  38.  
  39. Private Sub List1_Click()
  40. Set WS = DBEngine.Workspaces(0)
  41.     DbFile = (App.Path & "\Database\AddressBook.mdb")
  42.     PwdString = "swordfish"
  43. Set db = DBEngine.OpenDatabase(DbFile, False, False, ";PWD=" & PwdString)
  44. Set rs = db.OpenRecordset("Select * from Addresses where FullName = '" & Trim(List1.List(List1.ListIndex)) & "'")
  45. On Error GoTo ErrorHandler
  46. lblName.Caption = rs("FullName")
  47. lblNationality.Caption = rs("Nationality")
  48. lblCountry.Caption = rs("Country")
  49. lblBirthday.Caption = rs("Birthday")
  50. lblCountry.Caption = rs("Country")
  51. lblCity.Caption = rs("City")
  52. lblAddress.Caption = rs("Address")
  53. lblPhoneNumber.Caption = rs("PhoneNumber")
  54. lblMobile.Caption = rs("Mobile")
  55. lblFax.Caption = rs("Fax")
  56. lblEMAIL.Caption = rs("E-Mail")
  57. lblWebSite.Caption = rs("WebSite")
  58. lblCompany.Caption = rs("Company")
  59. Text1.Text = rs("LittleComment")
  60. ErrorHandler:
  61. Resume Next
  62. End Sub
Oct 19 '09 #1
1 2793
vb5prgrmr
305 Expert 100+
Expand|Select|Wrap|Line Numbers
  1. 'declare variables to be used and in this case these variables represent DAO (Data Access Object) objects although so you can understand more the order has been changed by me to more represent the heirarchy of the objects
  2. Dim WS As Workspace
  3. Dim db As Database
  4. Dim rs As Recordset
  5.  
  6. Dim Max As Long
  7.  
  8.  
  9. 'this even is called after initialize
  10. Private Sub Form_Load()
  11.  
  12. 'create the workspace via the dbengine (jet) object
  13. Set WS = DBEngine.Workspaces(0)
  14.  
  15. 'this example you have shown is an example of bad programming practice as none of these variables are declared. To remedy this place Option Explicit at top of code window prior to any declarations
  16. DbFile = (App.Path & "\Database\AddressBook.mdb")
  17. PwdString = "swordfish"
  18.  
  19. 'open the database
  20. Set db = DBEngine.OpenDatabase(DbFile, False, False, ";PWD=" & PwdString)
  21.  
  22. 'open a recordset, namely the addresses table
  23. Set rs = db.OpenRecordset("Addresses", dbOpenTable)
  24.  
  25. 'retrieve the number of records and display
  26. Max = rs.RecordCount
  27. lblTotalContacts.Caption = Max
  28.  
  29. 'check to see if there are any records
  30. If rs.RecordCount = 0 Then
  31.  
  32.   'no records so exit this sub
  33.   Exit Sub
  34. Else
  35.  
  36.   'have records so position the recordset cursor to the first record
  37.   rs.MoveFirst
  38.  
  39.   'clear list1
  40.   List1.Clear
  41.  
  42.   'now run through rs and add the information to list1
  43.   For i = 1 To Max
  44.     List1.AddItem rs!FullName
  45.     rs.MoveNext
  46.   Next i
  47.   List1.ListIndex = 0
  48. End If
  49.  
  50. 'call a sub that enables/disables buttons depending upon...?
  51. ButtonsEnabled
  52. End Sub
  53.  
  54. 'this event is called when the mouse moves over the form
  55. Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
  56. 'call sub...
  57. ButtonsEnabled
  58. End Sub
  59.  
  60. Private Sub Form_Unload(Cancel As Integer)
  61.  
  62. 'another example of bad programming practice with vb6 as this does not allow you to properly close your object that you have opened and may lead to memory leaks and system instability
  63. End
  64. End Sub
  65.  
  66. 'when user clicks on an item in list1 this event is called
  67. Private Sub List1_Click()
  68.  
  69. 'the following lines are not needed as the workspace is already created and the database is already opened...
  70. 'Set WS = DBEngine.Workspaces(0)
  71. 'DbFile = (App.Path & "\Database\AddressBook.mdb")
  72. 'PwdString = "swordfish"
  73. 'Set db = DBEngine.OpenDatabase(DbFile, False, False, ";PWD=" & PwdString)
  74.  
  75. 'open a recordset based upon user selection
  76. Set rs = db.OpenRecordset("Select * from Addresses where FullName = '" & Trim(List1.List(List1.ListIndex)) & "'")
  77.  
  78. 'tell compiler that we will attempt to handle any errors after this label below
  79. On Error GoTo ErrorHandler
  80.  
  81. 'another example of bad programming practice as these should be rs.fields("fieldname") for complete readability and for easier maintence by the noobie
  82. lblName.Caption = rs("FullName")
  83. lblNationality.Caption = rs("Nationality")
  84. lblCountry.Caption = rs("Country")
  85. lblBirthday.Caption = rs("Birthday")
  86. lblCountry.Caption = rs("Country")
  87. lblCity.Caption = rs("City")
  88. lblAddress.Caption = rs("Address")
  89. lblPhoneNumber.Caption = rs("PhoneNumber")
  90. lblMobile.Caption = rs("Mobile")
  91. lblFax.Caption = rs("Fax")
  92. lblEMAIL.Caption = rs("E-Mail")
  93. lblWebSite.Caption = rs("WebSite")
  94. lblCompany.Caption = rs("Company")
  95. Text1.Text = rs("LittleComment")
  96.  
  97. 'label for the error handler. Also, missing the exit sub statement before this line
  98. ErrorHandler:
  99.  
  100. 'basically we are telling the compiler that we are trying to ignore any errors and to go to the next line and if that is so then we could have used the On Error Resume Next statement instead of telling the system that we will try to handle the error
  101. Resume Next
  102. End Sub
Hope that helps

Good Luck
Oct 21 '09 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Colin MacDougall | last post by:
Hi, I am just starting to learn Visual Basic 6 and was wondering if just going straight on to VB.NET would be a better bet. Is VB.NET just a newer version of Visual Basic 6 or does it differ...
8
by: Will | last post by:
I just discovered Python and looked briefly at one of the tutorials for beginners... It looks a lot like the old Command line Basic... I'm sure it does much more but... 1 - Can you create...
4
by: hooi | last post by:
I'm new to Visual Studio.NET and Visual Basic.NET. While trying to run the sample application, it shows an error message as shown below. I have an SQL Server containing the sample database...
9
by: developer | last post by:
Does anyone know what is the way IE treats span tags(<span>) and table tags(<tr>, <td>)? Should the <span> tag be encolsed in tds and trs if it placed with other elements that are in a table? Can...
121
by: typingcat | last post by:
First of all, I'm an Asian and I need to input Japanese, Korean and so on. I've tried many PHP IDEs today, but almost non of them supported Unicode (UTF-8) file. I've found that the only Unicode...
3
by: Sathyaish | last post by:
I wanted to practice some Linked List stuff, so I set out to create a linked list. The plan was to create the following: (1) A linked list class in Visual Basic (2) A non-class based linked list...
41
by: Mountain Bikn' Guy | last post by:
What is the current preferred way to save user preferences in dotnet? Is the registry the right place to do this? Can anyone recommend a good article (or book) for this topic? Thanks.
17
by: Mike Hofer | last post by:
While I'd toyed with C, C++, and Java over the last 20 years or so, my principal language has been BASIC, QBASIC, then Visual Basic, and finally Visual Basic .NET. But lately, I've been using C#...
8
by: kevin | last post by:
I have a form and in the form I have a sub that uses a class I instantiate using visual basic code: Public oCP As New Rs232 'instantiate the comm port I need to share this sub with...
0
by: Dr. Zharkov | last post by:
Hello. To see the graphics of technology DirectX 9.0 SDK Update - June 2005 in project Visual Basic, WindowsApplication1 from Visual Studio 2005 Beta 1, in file My Project, MyApplication.vb in a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.