Stupid newbie question

  • Thread starter Thread starter Amy Blankenship
  • Start date Start date
A

Amy Blankenship

Hi, all;

I'm trying to add a button to a form that will let me run a sub that's
defined in a module. However, when I try to just type in the name of the
sub and its parameters, Access does not find the "Macro." I thought of
moving the sub to the macros section, but I'm not sure how to make that
work. The interface does not at all look like it's cut out for "real" code.
All the help and the interface seem directed at running something included
in a macro, but there's gotta be a way to run a defined sub from a form
button...I remember doing this years ago in Access 97.

Anyone want to take pity on me? I'm obviously missing something.

Thanks;

Amy
 
Amy Blankenship said:
Hi, all;

I'm trying to add a button to a form that will let me run a sub that's
defined in a module. However, when I try to just type in the name of
the sub and its parameters, Access does not find the "Macro." I
thought of moving the sub to the macros section, but I'm not sure how
to make that work. The interface does not at all look like it's cut
out for "real" code. All the help and the interface seem directed at
running something included in a macro, but there's gotta be a way to
run a defined sub from a form button...I remember doing this years
ago in Access 97.

Anyone want to take pity on me? I'm obviously missing something.

Thanks;

Amy

On the On Click event property line of the button's property sheet,
enter or choose

[Event Procedure]

Then click the "build" button (captioned "...") that appears at the end
of the line. That will build the shell of an event procedure for the
button's Click event. It will look something like this:

Private Sub Command1_Click()

End Sub

Add your call to the sub between those two lines. It might look roughly
like this:

Private Sub Command1_Click()

MySubName argumtent1, argument2

End Sub

This is an equivalent syntax:

Private Sub Command1_Click()

Call MySubName(argumtent1, argument2)

End Sub
 
Thanks!

-Amy

Dirk Goldgar said:
Amy Blankenship said:
Hi, all;

I'm trying to add a button to a form that will let me run a sub that's
defined in a module. However, when I try to just type in the name of
the sub and its parameters, Access does not find the "Macro." I
thought of moving the sub to the macros section, but I'm not sure how
to make that work. The interface does not at all look like it's cut
out for "real" code. All the help and the interface seem directed at
running something included in a macro, but there's gotta be a way to
run a defined sub from a form button...I remember doing this years
ago in Access 97.

Anyone want to take pity on me? I'm obviously missing something.

Thanks;

Amy

On the On Click event property line of the button's property sheet,
enter or choose

[Event Procedure]

Then click the "build" button (captioned "...") that appears at the end
of the line. That will build the shell of an event procedure for the
button's Click event. It will look something like this:

Private Sub Command1_Click()

End Sub

Add your call to the sub between those two lines. It might look roughly
like this:

Private Sub Command1_Click()

MySubName argumtent1, argument2

End Sub

This is an equivalent syntax:

Private Sub Command1_Click()

Call MySubName(argumtent1, argument2)

End Sub

--
Dirk Goldgar, MS Access MVP
www.datagnostics.com

(please reply to the newsgroup)
 
OK, I got that part working...now the code itself is failing. Here's what I
have (in part copied from a Word Add-in that works):

Sub GetFile(Control)
Dim retFile As String, dlg As Variant, s As String
Set dlg = Application.FileDialog(msoFileDialogFilePicker)
With dlg
If GetDirectory.InputDirectory.Value <> "" Then
.InitialFileName = GetDirectory.InputDirectory.Value
Else
.InitialFileName = CodeProject.Path
End If
If .Show = -1 Then s = .SelectedItems(1)
End With
If s <> "" Then
retFile = Right(s, Len(s) - InStrRev(s, "\"))
Control.Value = retFile
End If
End Sub

I had to change dlg as FileDialog to dlg as Variant, and the FileDialog
"method" of the Application object is failing. FileDialog definitely
appears in the object explorer, and the code I have agrees with the Access
help.

Any idea why Access won't pop up a FileDialog?

Thanks;

Amy

Dirk Goldgar said:
Amy Blankenship said:
Hi, all;

I'm trying to add a button to a form that will let me run a sub that's
defined in a module. However, when I try to just type in the name of
the sub and its parameters, Access does not find the "Macro." I
thought of moving the sub to the macros section, but I'm not sure how
to make that work. The interface does not at all look like it's cut
out for "real" code. All the help and the interface seem directed at
running something included in a macro, but there's gotta be a way to
run a defined sub from a form button...I remember doing this years
ago in Access 97.

Anyone want to take pity on me? I'm obviously missing something.

Thanks;

Amy

On the On Click event property line of the button's property sheet,
enter or choose

[Event Procedure]

Then click the "build" button (captioned "...") that appears at the end
of the line. That will build the shell of an event procedure for the
button's Click event. It will look something like this:

Private Sub Command1_Click()

End Sub

Add your call to the sub between those two lines. It might look roughly
like this:

Private Sub Command1_Click()

MySubName argumtent1, argument2

End Sub

This is an equivalent syntax:

Private Sub Command1_Click()

Call MySubName(argumtent1, argument2)

End Sub

--
Dirk Goldgar, MS Access MVP
www.datagnostics.com

(please reply to the newsgroup)
 
Amy Blankenship said:
OK, I got that part working...now the code itself is failing. Here's
what I have (in part copied from a Word Add-in that works):

Sub GetFile(Control)
Dim retFile As String, dlg As Variant, s As String
Set dlg = Application.FileDialog(msoFileDialogFilePicker)
With dlg
If GetDirectory.InputDirectory.Value <> "" Then
.InitialFileName = GetDirectory.InputDirectory.Value
Else
.InitialFileName = CodeProject.Path
End If
If .Show = -1 Then s = .SelectedItems(1)
End With
If s <> "" Then
retFile = Right(s, Len(s) - InStrRev(s, "\"))
Control.Value = retFile
End If
End Sub

I had to change dlg as FileDialog to dlg as Variant, and the
FileDialog "method" of the Application object is failing.
FileDialog definitely appears in the object explorer, and the code I
have agrees with the Access help.

Any idea why Access won't pop up a FileDialog?

If you set a reference (Tools -> References...) to the Microsoft Office
<version> Object Library, you can change the declaration of dlg back to
"... As FileDialog", and your use of the defind constant
msoFileDialogFilePicker will work. I have no idea, though, what
GetDirectory is. It's not a built-in Access property or method, so you
may fail there.
 
That's grayed out. I knew there was something like that lurking around, as
I vaguely recall having to change this before to make something else to
work, but how do I get the References option not to be grayed out?

Thanks;

Amy
 
Amy Blankenship said:
That's grayed out. I knew there was something like that lurking
around, as I vaguely recall having to change this before to make
something else to work, but how do I get the References option not to
be grayed out?

Are you currently running the code in break mode? If so, stop running
the code and see if the References... option is available.
 
Thanks, got it working now. The GetDirectory object was from the Word form
in the Add-in, so of course that stymied it too.

Thanks;

Amy
 
Back
Top