473,654 Members | 3,035 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

1 New Member
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 2801
vb5prgrmr
305 Recognized Expert Contributor
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
2619
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 radically? My main reason for programming in VB is to build a data logger that reads, processes and stores bytes from an external device through the serial port and then exports it to an
8
2286
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 windows, buttons, user input fields, etc as you can with Visual Basic? 2 - Can you call Windows Procedures or what ever they call them
4
5612
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 Northwind used in this application. Authentication mode set in Web.config is Windows; Mixed mode in the SQL Server. Any idea how I can resolve this problem? Thanks... Server Error in '/HowToBuildAWebDataEntryApp' Application....
9
7067
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 the span tag itself contain table tags within it? I have some scripting code and when I wrap the span in table elements it does not find the html within the span. Here is an example.... <tr><td colspan="4" align="left"><span...
121
10023
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 support IDEs are DreamWeaver 8 and Zend PHP Studio. DreamWeaver provides full support for Unicode. However, DreamWeaver is a web editor rather than a PHP IDE. It only supports basic IntelliSense (or code completion) and doesn't have anything...
3
2073
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 using functions in C (3) A linked list class in C++ I started with Visual Basic and I wrote an IList interface that I wanted my list to implement. When I had started, somehow I thought this time, I'd first use a collection as the ingredient,...
41
3416
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
2303
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# and I absolutely *love* it. It makes me think more about what I'm doing it before I just spew code into the editor. I'm writing better code than ever. The only thing so far that I don't like about it is the switch construct. I can't do this:
8
11435
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 another form so to declare the sub I use visual basic code: Public Shared Function IsPortAvailable(ByVal ComPort As Integer) As Boolean
0
1511
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 method: <Global.System.Diagnostics.DebuggerStepThrough()> _ Protected Overrides Sub OnCreateMainForm()
0
8376
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8815
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8489
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8594
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7307
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6161
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5622
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4149
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
1916
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.