Classes creation help

Posted on 2000-03-30
Medium Priority
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 80 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

585 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