Option Explicit help

K

Kingtriton

Hello,
I am trying to find a way to link an Option Explicit module to a butto
and cant seem to do it. I cant figure out a way to give an optio
explicit macro (module 11) to show up in a list of macros to link
button to. I am trying to make clicking a button to take the user to
secure area of my workbook prompt a password entry box. The code
found to do this with starts off with option explicit instead of Su
""(). How do I link this to a button?
Thanks in advance,
Kingtrito
 
R

Rob van Gelder

Option Explicit is cannot be attached to a button. It tells the VBA compiler
that every variable must be declared (Dimensioned).

The macros that you *can* attach start with a Sub (or Public Sub) and have
no arguments - that is, the brackets are empty.
eg.
Sub MyProcedure()

From Excel, press Alt+F8 to get a list of macros which you can attach.
 
K

Kingtriton

Is there a way to write a sub that will run an option explicit macro?
You know, write an normal sub that on button click will run module 11
option explicit. Can that be done
 
F

Frank Kabel

Hi
I think your're not quite understanding what 'Option Explicit' does.
It's just a compiler setting. You can invoke macros whether they are in
a module with this option enabled or not. no difference in calling such
macros or assigning them to a macro.
What 'Option Explicit' does is to tell the compiler to check that all
variables are declared before they are used in a macro. Using this
option prevents typos. Lets give you an example

Sub foo()
Dim ret_str as string
ret_str = "hello world"
msgbox retstr
end sub

without using 'Option explicit' the above procedure would show an empty
messagebox (as I made a typo in retstr). If you put 'Option Explicit'
at the beginning of this module the compiler would issue an error that
'retstr' is not declared
 
K

Kingtriton

What I am trying to do is to insert a userform text box that users wil
enter a password into to open areas of a workbook with sensitiv
information. Here is the code I am using
Option Explicit




'// My thanks to:
'// Ken Getz and Michael Kaplan
'// For their brilliant work with
'// Using Callbacks with Office 97
'// KNG Consulting, Inc.
'// Copyright © 1998
'// Ken Getz & Michael Kaplan
'// All rights reserved.
'-------------------------------------------------------------------------------------------------------------------
' Declarations
'
' These function names were puzzled out by using DUMPBIN /exports
' with VBA332.DLL and then puzzling out parameter names and types
' through a lot of trial and error and over 100 IPFs in MSACCESS.EXE
' and VBA332.DLL.
'
' These parameters may not be named properly but seem to be correc
in
' light of the function names and what each parameter does.
'
' EbGetExecutingProj: Gives you a handle to the current VBA project
' TipGetFunctionId: Gives you a function ID given a function name
' TipGetLpfnOfFunctionId: Gives you a pointer a function given it
function ID
'
'-------------------------------------------------------------------------------------------------------------------
Private Declare Function GetCurrentVbaProject _
Lib "vba332.dll" _
Alias "EbGetExecutingProj" ( _
hProject As Long) _
As Long

Private Declare Function GetFuncID _
Lib "vba332.dll" _
Alias "TipGetFunctionId" ( _
ByVal hProject As Long, _
ByVal strFunctionName As String, _
ByRef strFunctionId As String) _
As Long

Private Declare Function GetAddr _
Lib "vba332.dll" _
Alias "TipGetLpfnOfFunctionId" ( _
ByVal hProject As Long, _
ByVal strFunctionId As String, _
ByRef lpfn As Long) _
As Long

'-------------------------------------------------------------------------------------------------------------------
' AddrOf
'
' Returns a function pointer of a VBA public function given its name
This function
' gives similar functionality to VBA as VB5 has with the AddressO
param type.
'
' NOTE: This function only seems to work if the proc you are tryin
to get a pointer
' to is in the current project. This makes sense, since we ar
using a function
' named EbGetExecutingProj.
'-------------------------------------------------------------------------------------------------------------------
Public Function AddrOf(strFuncName As String) As Long
Dim hProject As Long
Dim lngResult As Long
Dim strID As String
Dim lpfn As Long
Dim strFuncNameUnicode As String

Const NO_ERROR = 0

' The function name must be in Unicode, so convert it.
strFuncNameUnicode = StrConv(strFuncName, vbUnicode)

' Get the current VBA project
' The results of GetCurrentVBAProject seemed inconsistent, in ou
tests,
' so now we just check the project handle when the functio
returns.
Call GetCurrentVbaProject(hProject)

' Make sure we got a project handle... we always should, but yo
never know!
If hProject <> 0 Then
' Get the VBA function ID (whatever that is!)
lngResult = GetFuncID( _
hProject, strFuncNameUnicode, strID)

' We have to check this because we GPF if we try to get
function pointer
' of a non-existent function.
If lngResult = NO_ERROR Then
' Get the function pointer.
lngResult = GetAddr(hProject, strID, lpfn)

If lngResult = NO_ERROR Then
AddrOf = lpfn
End If
End If
End If
End Function

How do I make it so that clicking a button will bring up the passwor
promt and then, if entered correctly, take the user to whatever area o
my workbook they are trying to veiw?
Thanks
 
C

Chip Pearson

As far as I can tell, all the code you posted is completely
irrelevant to the question at hand. Why don't you just create a
simple userform with a textbox (with the PasswordChar property
properly set, if desired) and command button. Then, in the
command button's click event, test the password and act
accordingly:

Private Sub Textbox1_Click()
If Me.Textbox1.Text = "the_correct_password" Then
' do something
Else
Msgbox "Invalid Password"
End If
End Sub


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top