Results 1 to 6 of 6

Thread: Please Help, i'm newbie

  1. #1
    Guest

    Question

    i'm using ADO and wamt to simplify this:
    adodc1.recordset.movenext

    in the load module i have

    Private Sub Form_Load()

    Dim rs As Recordset
    Set rs = Adodc1.recordset

    End Sub

    Private Sub Button_Next()

    rs.movenext

    End Sub

    why this code don't work??


    TIA

  2. #2
    Lively Member
    Join Date
    Oct 2000
    Location
    Leicestershire; ENGLAND
    Posts
    71
    It is a question of scope:

    the variable "rs" is defined within the form_load() event, and therefore can only be accessed within that subroutine. You need to declare the "rs" variable at a module level.
    Re-code your form as follows:

    option explicit

    dim rs as adodb.recordset

    private sub form_load()
    set rs = adodc1.recordset
    end sub

    private sub button_next()
    rs.movenext
    end sub

  3. #3
    Guest

    Thumbs up

    Thank you very much Orpheus, one more question the "Option Explicit" is the place to declare all the variables???

  4. #4
    Member
    Join Date
    May 2000
    Location
    Turkey
    Posts
    49
    Hi JuanCarlos60,

    The answer is No. "OPTION EXPLICIT" statement is not the way to declare the all variables.

    It is used at module level to force explicit declaration of all variables in that module.

    When used, if you attempt to use an undeclared variable name, an error occurs at compile time.

    If you don't use the Option Explicit statement, all undeclared variables are of Variant type unless the default type is otherwise specified with a Deftype statement.

    So you must always use OPTION EXPLICIT to prevent variable-based errors and it is very useful because it forces to declare all the variables. It is necessary for the performance point of view.

    Good Luck...

  5. #5
    Fanatic Member
    Join Date
    Oct 2000
    Location
    London
    Posts
    1,008

    Thumbs up Declaring...

    I think leontro has slightly misunderstood.

    If you declare the rs recordset just after the Option Explicit statement then it is declared at module level (Form, Class or BAS). This means it is available to all procedures in that form. Since you are binding something to the recordset, you want it to have the same life timeas the form so YES, just after the Option Explicit but outside any procedures.

    Cheers.

    Paul.
    Not nearly so tired now...

    Haven't been around much so be gentle...

  6. #6
    Lively Member
    Join Date
    Oct 2000
    Location
    Leicestershire; ENGLAND
    Posts
    71
    Both leontro and paulw are correct, however, perhaps some clarification would help.

    The stetment "Option Explicit" forces VB to make you declare all variables within your program before you use them. This in turn means that at any time that you use one of these variables, the type of variable will be checked against the usage at *compile* time. Hopefully, this will help prevent errors occurring for your users.

    The fact that I placed the declaration of "rs" immediately after the "Option Explicit" statement was in order to decare the variable at a module level; ie: I wanted every procedure in the form to be able to see the variable "rs", this being your original problem.

    I always use an "Option Explicit" statement in every module, even those that I write to try and resolve issues that other users within the forums that I visit are having. I *personally* consider it to be the correct programming practice <perhaps this is due to a background in C and C++>. I am sorry for the confusion that I may have caused, the revised code segment hat I provided would have worked with or without the "Option Explicit" statement.

    I still believe however that *every* VB programmer should start each module/form/class etc. with "Option Explicit", it *will* save headaches later. This can also be set as a default within the VB IDE.

    Cheers guys ...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width