Classes creation help

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.
Who is Participating?
razorzejConnect With a Mentor Commented:
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.
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!

vbRetardAuthor Commented:
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 ?
vbRetardAuthor Commented:
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 ?
All Courses

From novice to tech pro — start learning today.