PDA

Click to See Complete Forum and Search --> : Please Help, i'm newbie


Nov 2nd, 2000, 06:42 PM
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

Orpheus
Nov 3rd, 2000, 04:59 AM
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

Nov 3rd, 2000, 05:44 AM
Thank you very much Orpheus, one more question the "Option Explicit" is the place to declare all the variables???

leontro
Nov 3rd, 2000, 06:04 AM
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...

paulw
Nov 3rd, 2000, 07:42 AM
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.

Orpheus
Nov 4th, 2000, 12:33 PM
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 ...