Classes creation help

Posted on 2000-03-30
Last Modified: 2010-05-02
Hi everyone. I am new to objects and trying to understand them. I need to
create a Registration form. Where the user can come and register. 1st i need
to see if the user name already exists in a database. 2nd to validate his
entry and 3rd to record it into database. Would anyone have ideas how i can
do this ? I am using control array of 4 textboxes on the form. The data will
be taken from there to be validated. I though i would create a method called.
Varify which will first see if the uid is already taken. Then i would check
to see if everything was filled out and passwords match. and 3rd then call
Record method to insert the new record. Would this be a good approach ? Or
someone has suggestions how i can do this. I'd like to learn this. Thanks
a lot for your time and help.
Question by:vbRetard
  • 2
  • 2

Expert Comment

ID: 2671842
I think that you can use a simpler approach.

Create a class to encapsulate the logic of registering a user. You could name it "CRegister", and save it as "Register.cls"

This class can expose a single method (a function) that can return infomation to the calling code, e.g.:

' If successful, returns True, and sets
' sErrorMsg to a blank string. If fails,
' returns False and sets sErrorMsg to
' an error message.
Public Function RegisterUser(sUserID as string, sPassword1 as string, sPassword2 as string, byref sErrorMsg as string) as boolean
on error goto ErrorHandler

    ' Step 1: Check the passwords match
    If sPassword1 <> sPassword then
        sErrorMsg = "Passwords don't match."
        RegisterUser = False
        Exit Function
    End if

    ' Step 2: Check the user id and password are both non-blank:
     If sPassword1 = "" then
        sErrorMsg = "Please enter a password."
        RegisterUser = False
        Exit Function

     End if

    If Trim$(sUserID) = "" then
        sErrorMsg = "Please enter a user id."
        RegisterUser = False
        Exit Function
    end if  

    ' I assume that you will be using
    ' a database table to store the
    ' user id & password. Also, this
    ' will use the user id as the
    ' primary key of this table.

    ' You can combine the testing for
    ' a unique user id and the attempt
    ' to add it using a single step
    ' (SQL insert).
    ' If you attempt to register    
    ' (insert) using a user id that
    ' is in use, the database will
    ' generate an error. If you detect
    ' this error, you can tell the
    ' user to choose another id.

    ' I assume that you know how to
    ' set up an ADO connection to your
    ' database, and to create a
    ' reference to ADO in your VB  
    ' project.

    Dim sSQL as string
    Dim CN as New ADODB.Connection

    CN.ConnectionString = "...whatever your settings are..."    

    on error Resume Next

    sSQL = "INSERT INTO TableName " & _
           "(UserIDField, PasswordField) & _
           "VALUES ('" & sUserID & "', '" & sPassword1 & "')

    CN.Execute sSQL

    If Err.number <> 0 then
        sErrorMsg = "Please choose another user id."
        RegisterUser = False
        Exit Function
    end if

RegisterUser = True

Exit Function    
    sErrorMsg = "Unexpected error."
    RegisterUser = False

End Function  

--- Now, in the calling code ---

I guess you have a Submit button:

Private Sub cmdSubmitClick()

    dim bResult as boolean
    dim sErrorMsg as string
    Dim clsReg as New CRegister

    bResult = clsReg.RegisterUser(txtInfo(0), txtInfo(1), txtInfo(2)m sErrorMsg)

    if not bresult then
        msgbox serrmsg
       msgbox "User registered!"
End Sub

' This should get you going!


Author Comment

ID: 2673023
Thanks its a good answer i will accpet it as an answer i just want to see if any one else can post a reply to this. Ok ?

Author Comment

ID: 2675218
May i ask you something else ? Is this a good idea to create this class or does it look like a waist of time to do something simple as this ? Or would this actually do good too my program ?

Accepted Solution

razorzej earned 20 total points
ID: 2677474
The advantage of classes is that you can encapsulate both the behaviour and the data belonging to an object. You can define an interface that allows people to see certain functionality, whilst hiding from them the inner workings.

Classes are especially useful when you want to reuse code in other projects. The .cls file can often be dropped into another project, and (depending on its dependencies) you know that all of the required plumbing is inside the class.

When used properly, classes can make your program more efficient memory wise, as they are only loaded when required (like forms).

The problem with BAS modules, is that they are loaded all the time, consuming lots of memory. Public variables in BAS files are global to the project, and the values can be overwritten from anywhere. This is a programming practice to be avoided, and maintenance becomes harder.

I think it is worthwhile learning about using classes, as they are essential to more advanced stuff which you will learn about eventually, like add-ins, DLL's, collection classes, data-aware classes, object models and all sorts of other stuff.

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now