Execute a string

Pyro

Too busy to comment
Local time
Tomorrow, 07:56
Joined
Apr 2, 2009
Messages
127
Hello,

I have spent a couple of hours now playing around with this, and i can't seem to get it to work.

Code:
'callback for OnButtonPress
Public Sub OnButtonPress(ctl As IRibbonControl)
 
Dim obj As Object
Dim StrName As String
 
If CurrentProject.AllForms(ctl.Tag).IsLoaded Then
    'Do nothing
 
Else
    For Each obj In Application.CurrentProject.AllForms
        If CurrentProject.AllForms(obj.Name).IsLoaded Then
            strName = strName & "Forms!" & obj.Name & ".Visible = False"
 
            *run this string* (strName)
 
        End If
 
    strName = ""
 
    Next obj
 
    DoCmd.OpenForm ctl.Tag
 
End If
 
End Sub

I am just trying to make any open forms invisible when the button is pressed. I have tried Eval() which does nothing. Any help would be received with relief!
 
Try;

Code:
'callback for OnButtonPress
Public Sub OnButtonPress(ctl As IRibbonControl)
 
Dim obj As Object
Dim StrName As String
 
If CurrentProject.AllForms(ctl.Tag).IsLoaded Then
    'Do nothing
 
Else
    For Each obj In Application.CurrentProject.AllForms
        If CurrentProject.AllForms(obj.Name).IsLoaded Then
 
           Forms( obj.Name ).Visible = False
 
        End If
 
 
    Next obj
 
    DoCmd.OpenForm ctl.Tag
 
End If
 
End Sub

or

If it were me I would use something like this:

Code:
'callback for OnButtonPress
Public Sub OnButtonPress(ctl As IRibbonControl)
 
Dim obj As Object
Dim StrName As String
 
If CurrentProject.AllForms(ctl.Tag).IsLoaded Then
    'Do nothing
 
Else
    For Each obj In Application.CurrentProject.AllForms
  
           Forms( obj.Name ).Visible = Not CurrentProject.AllForms(obj.Name).IsLoaded
 
    Next obj
 
    DoCmd.OpenForm ctl.Tag
 
End If
 
End Sub
 
Brilliant!

Believe it or not i had that almost exactly, but Access kept freaking out because i was trying:

Code:
Forms!(obj.name).visible...

All i had to do was remove the apostophe!!

Well here is the working code. It links to a custom button on the ribbon (Access v. 2k7) and sets all open forms' visible property to false and either opens or sets visible the form listed in the buttons Tag property.

Code:
'Callback for OnButtonPress
Public Sub OnButtonPress(ctl As IRibbonControl)
 
Dim obj As Object
Dim strName As String
 
'Determine if the form is loaded.
If CurrentProject.AllForms(ctl.Tag).IsLoaded Then
 
    'Determine if the form is visible.
    If Forms(ctl.Tag).Visible = False Then
 
        'If the form is loaded and its visibility is false, set all other forms visibility to false, and set it's visibility to true.
        For Each obj In Application.CurrentProject.AllForms
            If CurrentProject.AllForms(obj.Name).IsLoaded Then
                Forms(obj.Name).Visible = Not CurrentProject.AllForms(obj.Name).IsLoaded
 
            End If
 
        Next obj
 
        Forms(ctl.Tag).Visible = True
 
    Else
        'Do nothing.
 
    End If
 
Else 'The form must not be open.
    'Set all other forms visibility to false.
    For Each obj In Application.CurrentProject.AllForms
        If CurrentProject.AllForms(obj.Name).IsLoaded Then
            Forms(obj.Name).Visible = Not CurrentProject.AllForms(obj.Name).IsLoaded
 
        End If
 
    Next obj
 
    'Open the form
    DoCmd.OpenForm ctl.Tag
 
End If
 
End Sub
 

Users who are viewing this thread

Back
Top Bottom