-
Dim db As Database
-
Dim rs As Recordset
-
Dim WS As Workspace
-
Dim Max As Long
-
-
-
Private Sub Form_Load()
-
Set WS = DBEngine.Workspaces(0)
-
DbFile = (App.Path & "\Database\AddressBook.mdb")
-
PwdString = "swordfish"
-
Set db = DBEngine.OpenDatabase(DbFile, False, False, ";PWD=" & PwdString)
-
Set rs = db.OpenRecordset("Addresses", dbOpenTable)
-
Max = rs.RecordCount
-
lblTotalContacts.Caption = Max
-
If rs.RecordCount = 0 Then
-
Exit Sub
-
Else
-
rs.MoveFirst
-
-
List1.Clear
-
-
For i = 1 To Max
-
List1.AddItem rs!FullName
-
rs.MoveNext
-
Next i
-
List1.ListIndex = 0
-
End If
-
ButtonsEnabled
-
End Sub
-
-
Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
-
ButtonsEnabled
-
End Sub
-
-
Private Sub Form_Unload(Cancel As Integer)
-
End
-
End Sub
-
-
Private Sub List1_Click()
-
Set WS = DBEngine.Workspaces(0)
-
DbFile = (App.Path & "\Database\AddressBook.mdb")
-
PwdString = "swordfish"
-
Set db = DBEngine.OpenDatabase(DbFile, False, False, ";PWD=" & PwdString)
-
Set rs = db.OpenRecordset("Select * from Addresses where FullName = '" & Trim(List1.List(List1.ListIndex)) & "'")
-
On Error GoTo ErrorHandler
-
lblName.Caption = rs("FullName")
-
lblNationality.Caption = rs("Nationality")
-
lblCountry.Caption = rs("Country")
-
lblBirthday.Caption = rs("Birthday")
-
lblCountry.Caption = rs("Country")
-
lblCity.Caption = rs("City")
-
lblAddress.Caption = rs("Address")
-
lblPhoneNumber.Caption = rs("PhoneNumber")
-
lblMobile.Caption = rs("Mobile")
-
lblFax.Caption = rs("Fax")
-
lblEMAIL.Caption = rs("E-Mail")
-
lblWebSite.Caption = rs("WebSite")
-
lblCompany.Caption = rs("Company")
-
Text1.Text = rs("LittleComment")
-
ErrorHandler:
-
Resume Next
-
End Sub
1 2793 -
'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
-
Dim WS As Workspace
-
Dim db As Database
-
Dim rs As Recordset
-
-
Dim Max As Long
-
-
-
'this even is called after initialize
-
Private Sub Form_Load()
-
-
'create the workspace via the dbengine (jet) object
-
Set WS = DBEngine.Workspaces(0)
-
-
'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
-
DbFile = (App.Path & "\Database\AddressBook.mdb")
-
PwdString = "swordfish"
-
-
'open the database
-
Set db = DBEngine.OpenDatabase(DbFile, False, False, ";PWD=" & PwdString)
-
-
'open a recordset, namely the addresses table
-
Set rs = db.OpenRecordset("Addresses", dbOpenTable)
-
-
'retrieve the number of records and display
-
Max = rs.RecordCount
-
lblTotalContacts.Caption = Max
-
-
'check to see if there are any records
-
If rs.RecordCount = 0 Then
-
-
'no records so exit this sub
-
Exit Sub
-
Else
-
-
'have records so position the recordset cursor to the first record
-
rs.MoveFirst
-
-
'clear list1
-
List1.Clear
-
-
'now run through rs and add the information to list1
-
For i = 1 To Max
-
List1.AddItem rs!FullName
-
rs.MoveNext
-
Next i
-
List1.ListIndex = 0
-
End If
-
-
'call a sub that enables/disables buttons depending upon...?
-
ButtonsEnabled
-
End Sub
-
-
'this event is called when the mouse moves over the form
-
Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
-
'call sub...
-
ButtonsEnabled
-
End Sub
-
-
Private Sub Form_Unload(Cancel As Integer)
-
-
'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
-
End
-
End Sub
-
-
'when user clicks on an item in list1 this event is called
-
Private Sub List1_Click()
-
-
'the following lines are not needed as the workspace is already created and the database is already opened...
-
'Set WS = DBEngine.Workspaces(0)
-
'DbFile = (App.Path & "\Database\AddressBook.mdb")
-
'PwdString = "swordfish"
-
'Set db = DBEngine.OpenDatabase(DbFile, False, False, ";PWD=" & PwdString)
-
-
'open a recordset based upon user selection
-
Set rs = db.OpenRecordset("Select * from Addresses where FullName = '" & Trim(List1.List(List1.ListIndex)) & "'")
-
-
'tell compiler that we will attempt to handle any errors after this label below
-
On Error GoTo ErrorHandler
-
-
'another example of bad programming practice as these should be rs.fields("fieldname") for complete readability and for easier maintence by the noobie
-
lblName.Caption = rs("FullName")
-
lblNationality.Caption = rs("Nationality")
-
lblCountry.Caption = rs("Country")
-
lblBirthday.Caption = rs("Birthday")
-
lblCountry.Caption = rs("Country")
-
lblCity.Caption = rs("City")
-
lblAddress.Caption = rs("Address")
-
lblPhoneNumber.Caption = rs("PhoneNumber")
-
lblMobile.Caption = rs("Mobile")
-
lblFax.Caption = rs("Fax")
-
lblEMAIL.Caption = rs("E-Mail")
-
lblWebSite.Caption = rs("WebSite")
-
lblCompany.Caption = rs("Company")
-
Text1.Text = rs("LittleComment")
-
-
'label for the error handler. Also, missing the exit sub statement before this line
-
ErrorHandler:
-
-
'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
-
Resume Next
-
End Sub
Hope that helps
Good Luck
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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.
|
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#...
|
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...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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...
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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,...
|
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...
|
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...
| |