Insert a macro into VBA code

  • Thread starter Tom K via AccessMonster.com
  • Start date
T

Tom K via AccessMonster.com

I have a main form with 2 subforms. I need to include a macro in the second
form that checks some controls for null values. The problem is that in that
form there is allready an event procedure (code) in the on current event. It
is a VBA procedure that the wizard puts in when it makes the main form and 2
subforms. I dont know how to code VBA. I was hoping to put in a macro in the
on current event. Now the only way to do it is to include it into the VBA
that is allready there. I dont know how to incorporate the macro and the code.


I made the macro, and converted it into a module, but now I dont know what to
do next.
How do I combine the two?

Thanks,
Tom
 
T

Tom K via AccessMonster.com

This is what I need to insert.......

Function mcrEnabledControlsfrmBusiness()
On Error GoTo mcrEnabledControlsfrmBusiness_Err

If (Eval("([Forms]![frmAddress]![frmBusinessSubform].[Form]![AddressID])
Is Null")) Then
' Not enabled AddressID
Forms!frmAddress!frmBusinessSubform.Form!AddressID.Enabled = False
' Not enabled BusinessID
Forms!frmAddress!frmBusinessSubform.Form!BusinessID.Enabled = False
' Not enabled BusinessName
Forms!frmAddress!frmBusinessSubform.Form!BusinessName.Enabled = False
' Not enabled BusinessPhone
Forms!frmAddress!frmBusinessSubform.Form!BusinessPhone.Enabled =
False
End If
If (Eval("([Forms]![frmAddress]![frmBusinessSubform].[Form]![AddressID])
Is Not Null")) Then
' Enabled AddressID
Forms!frmAddress!frmBusinessSubform.Form!AddressID.Enabled = True
' Enabled BusinessID
Forms!frmAddress!frmBusinessSubform.Form!BusinessID.Enabled = True
' Enabled BusinessName
Forms!frmAddress!frmBusinessSubform.Form!BusinessName.Enabled = True
' Enabled BusinessPhone
Forms!frmAddress!frmBusinessSubform.Form!BusinessPhone.Enabled = True
End If


mcrEnabledControlsfrmBusiness_Exit:
Exit Function

mcrEnabledControlsfrmBusiness_Err:
MsgBox Error$
Resume mcrEnabledControlsfrmBusiness_Exit

End Function

This is what is allready there.......

Sub Form_Current()

Dim ParentDocName As String

On Error Resume Next
ParentDocName = Me.Parent.Name

If Err <> 0 Then
GoTo Form_Current_Exit
Else
On Error GoTo Form_Current_Err
Me.Parent![frmSIPSubform].Requery
End If

Form_Current_Exit:
Exit Sub

Form_Current_Err:
MsgBox Error$
Resume Form_Current_Exit

End Sub

Thanks for any help.

Tom
 
V

Vincent Johns

Tom said:
I have a main form with 2 subforms. I need to include a macro in the second
form that checks some controls for null values. The problem is that in that
form there is allready an event procedure (code) in the on current event. It
is a VBA procedure that the wizard puts in when it makes the main form and 2
subforms. I dont know how to code VBA. I was hoping to put in a macro in the
on current event. Now the only way to do it is to include it into the VBA
that is allready there. I dont know how to incorporate the macro and the code.


I made the macro, and converted it into a module, but now I dont know what to
do next.
How do I combine the two?

Thanks,
Tom

I hope my answer doesn't discourage someone who might be able to give
you more detail, but why don't you just add a "RunCode" action to run
the VBA code that is currently called by OnCurrent? The rest of your
macro you can include either before or after that. Then you wouldn't
have to write or maintain any VBA code.

-- Vincent Johns <[email protected]>
Please feel free to quote anything I say here.
 
G

Graham R Seach

Tom,

Off the top of my head, this might be simpler:

Sub EnabledControlsfrmBusiness()
Dim blnResult As Boolean

blnResult = IsNull(Me!frmBusinessSubform.Form!AddressID)

With Me!frmBusinessSubform.Form
!AddressID.Enabled = Not blnResult
!BusinessID.Enabled = Not blnResult
!BusinessName.Enabled = Not blnResult
!BusinessPhone.Enabled = Not blnResult
End With
Exit Sub

Regards,
Graham R Seach
Microsoft Access MVP
Sydney, Australia
 
T

Tom K via AccessMonster.com

I like the idea of not writing or maintaining any VBA code. Ive tried and
tried to figure out how to do what you suggest but with no luck. Do I need to
convert the code from the on current event procedure to a function? And then
use RunCode in a macro to run it? Do I put it into a module?

Im sorry Im so lost, but I just got the hang of macros and VBA is way over my
head right now.

I just want to run my macro from the on current event. It just happens that
the wizard put code on it and now I cant run my macro from there.

Thanks for the help.

Tom

Vincent said:
I have a main form with 2 subforms. I need to include a macro in the second
form that checks some controls for null values. The problem is that in that
[quoted text clipped - 10 lines]
Thanks,
Tom

I hope my answer doesn't discourage someone who might be able to give
you more detail, but why don't you just add a "RunCode" action to run
the VBA code that is currently called by OnCurrent? The rest of your
macro you can include either before or after that. Then you wouldn't
have to write or maintain any VBA code.

-- Vincent Johns <[email protected]>
Please feel free to quote anything I say here.
 
V

Vincent Johns

Tom said:
I like the idea of not writing or maintaining any VBA code. Ive tried and
tried to figure out how to do what you suggest but with no luck. Do I need to
convert the code from the on current event procedure to a function? And then
use RunCode in a macro to run it? Do I put it into a module?

Sorry about the slow response, but I ran Wizards to set up a Form and a
Subform, and I wasn't able to reconstruct what you described.

My guess is that the OnCurrent code is already in the form of a public
function, and that you need only copy that into a Module (if it's not
there already) to be able to invoke it via a Macro with a RunCode action.
Im sorry Im so lost, but I just got the hang of macros and VBA is way over my
head right now.

I just want to run my macro from the on current event. It just happens that
the wizard put code on it and now I cant run my macro from there.

Having copied what the wizard put into the OnCurrent Event to the line
for a new action in your Macro, I think you should be able to put the
name of your Macro into the OnCurrent event.
Thanks for the help.

Tom

[...]
 

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