Solved

Classes creation help

Posted on 2000-03-30
4
138 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

747 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

10 Experts available now in Live!

Get 1:1 Help Now