Solved

Classes creation help

Posted on 2000-03-30
4
140 Views
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.
0
Comment
Question by:vbRetard
  • 2
  • 2
4 Comments
 

Expert Comment

by:razorzej
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..."    
    CN.Open

    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    
ErrorHandler:
    Err.Clear
    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
    else
       msgbox "User registered!"
    endif
End Sub


' This should get you going!

RazorzEj
0
 

Author Comment

by:vbRetard
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 ?
0
 

Author Comment

by:vbRetard
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 ?
0
 

Accepted Solution

by:
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.
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

786 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