Convert string to a Form object

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • TravelingCat
    New Member
    • Feb 2010
    • 82

    Convert string to a Form object

    Hi,
    I found some material about this topic but it all refered to vb6.
    I have a table which contains names of the forms in my system. After getting the form name out of the table, i want to set the "AllowEdits " property of that form to false, but for that i need to convert the string which contains the form name into a Form object, at least so i think.. here is a piece of my code:
    Code:
    Do While Not rs.EOF
        Select Case rs("formNum")
        Case 1:
        rsForms.FindFirst ("formNum = ") & rs("formNum")
        If rsForms.NoMatch = False Then
            strFormName = rsForms("formEnglishName")
            [B]strFormName.AllowEdits = False[/B]
            ....
    strFormName holds the form name, but what should i do in order for the last line (in bold) to work?
    Thanks in advance
  • Megalog
    Recognized Expert Contributor
    • Sep 2007
    • 378

    #2
    Are you delcaring strFormName as a form type, not a string?

    Like:
    Code:
    Dim strFormName As Form

    Comment

    • TravelingCat
      New Member
      • Feb 2010
      • 82

      #3
      Originally posted by Megalog
      Are you delcaring strFormName as a form type, not a string?

      Like:
      Code:
      Dim strFormName As Form
      Hi Megalog,
      I actually declared it as string. That way i can assign it the form name, but can't assign form property (such as AllowEdits). And if i declare it as Form, than i can assign the property but don't have the name..

      Comment

      • NeoPa
        Recognized Expert Moderator MVP
        • Oct 2006
        • 32668

        #4
        There are collections available that allow you to pass a string parameter to select the matching item.
        Code:
        {Application.}Forms	 All currently open forms.
        CurrentProject.AllForms All forms in the project.
        Forms("YourForm Name") returns that form as long as it's already open and available. The {Application.} part is optional.

        Comment

        • Megalog
          Recognized Expert Contributor
          • Sep 2007
          • 378

          #5
          This can go in after line 5 from your initial posting:

          Code:
          DoCmd.OpenForm rsForms!formEnglishName, acDesign
          If Forms(rsForms!formEnglishName).AllowEdits = True Then
              Forms(rsForms!formEnglishName).AllowEdits = False
          End If
          DoCmd.Close acForm, rsForms!formEnglishName, acSaveYes

          Comment

          • TheSmileyCoder
            Recognized Expert Moderator Top Contributor
            • Dec 2009
            • 2322

            #6
            Just out of curiosity what are you using this for?

            Comment

            • TravelingCat
              New Member
              • Feb 2010
              • 82

              #7
              Convert string to a Form object

              Wow, it works! Thank you Megalog!:) I just added acHidden to window mode, so it won't pop.
              TheSmileyOne, i'm trying to implement restrictions for users. I created a form with a listbox filled with form names, so that whoever sets the configurations will choose set of forms for each worker, which he will not be allowed to modify. It's all my creation, so i'm sure it's not the most clever way, and i'm sure it will be modified not once, but at least what i have now works, thanks again.
              Now i'm thinking about a way to add an option to choose exactly what restriciton will be implemented for each form, something like checkboxes for edits/additions/deletions.

              Comment

              • TravelingCat
                New Member
                • Feb 2010
                • 82

                #8
                One more question

                Is there a way to remember listbox selection?
                The process is: I choose some values (form names) in the listbox, press button "save choice", then my code activates and the values go in the table as supposed to, but the next time i enter the form the listbox is fresh, and i want it to be exactly as it was when i left it (the chosen lines must be black). I thought maybe on form Load event go through the table which holds the chosen values, and make them appear as chosen, but how?

                Comment

                • TheSmileyCoder
                  Recognized Expert Moderator Top Contributor
                  • Dec 2009
                  • 2322

                  #9
                  This is some code I wrote myself, to convert the stored values into a comma delimite list, and store it in a hidden textbox, bound to a hidden TEXT field. (of course this only works if the length of the string wont exceed 255)

                  I only use this in a search form, to remember the users last performed search, so its not critical. If its something you need to store what user can access, I might want to write the data to tables instead of a string.

                  Anyways, here it is, and im sure you can modify it to read/restore the data of the table.
                  To STORE the values
                  Code:
                  Private Sub list_Status_BeforeUpdate(Cancel As Integer)
                      
                      StoreList Me.list_Status, Me.tb_status
                  
                  End Sub
                  To RESTORE the values
                  Code:
                  Private Sub Form_Current()
                  'Restore the list selections
                      restoreList Me.list_Status, Me.tb_status
                      restoreList Me.list_Grade, Me.tb_Severity
                  
                  End Sub

                  The procedures used:
                  Code:
                  Public Sub StoreList(ctrlListBox As Control, ctrlTextBox As Control)
                  'Lets store the items selected in the listbox
                      'Check that ctrlListBox is truly a listbox
                      If ctrlListBox.ControlType <> acListBox Then
                          MsgBox "This is not a listbox!"
                          Exit Sub
                      End If
                      
                      'Check that ctrlTextbox is truly a textbox
                      If ctrlTextBox.ControlType <> acTextBox Then
                          MsgBox "This is not a textbox!"
                          Exit Sub
                      End If
                      
                      'Dim variables
                          Dim lvItem As Variant
                          Dim intI As Integer
                          Dim strStorage As String
                      
                      'Loop through selected items
                          For Each lvItem In ctrlListBox.ItemsSelected
                              strStorage = strStorage & "," & ctrlListBox.ItemData(lvItem)
                          Next
                          
                      'If no items are selected then
                          If strStorage & "" <> "" Then
                              ctrlTextBox = Right(strStorage, Len(strStorage) - 1)
                              Else
                              ctrlTextBox = Null
                          End If
                      
                      
                      Set lvItem = Nothing
                      
                  End Sub
                  Code:
                  Public Sub restoreList(ctrlListBox As Control, ctrlTextBox As Control)
                      'Load the listbox
                      On Error GoTo exitSub
                      
                      'Init variables
                          Dim intI As Integer
                          Dim intCount As Integer
                          Dim myVar As Variant
                      'Unmark all
                          DoCmd.Echo (False) 'Turn of painting
                          For intI = 0 To ctrlListBox.ListCount
                              ctrlListBox.Selected(intI) = False
                          Next
                          
                      'If nothing is selected, go ahead and end sub
                          If ctrlTextBox & "" = "" Then GoTo endSub
                      
                      'How many items are marked as selected
                          intCount = CharCount(ctrlTextBox, ",")
                      
                      Dim intJ As Integer
                      'Restore teh selections
                          For intI = 0 To intCount
                              myVar = CVar(Split(ctrlTextBox, ",")(intI))
                              For intJ = 0 To ctrlListBox.ListCount
                                  If ctrlListBox.ItemData(intJ) = myVar Then
                                      ctrlListBox.Selected(intJ) = True
                                  End If
                                  
                              Next
                          Next
                  
                  endSub:
                      DoCmd.Echo (True)
                  
                  Exit Sub
                  exitSub:
                  Debug.Print Err.Number & " - " & Err.Description
                  DoCmd.Echo (True)
                  
                  End Sub

                  Comment

                  • Megalog
                    Recognized Expert Contributor
                    • Sep 2007
                    • 378

                    #10
                    If you're using A2007, you can create a multivalue field in the table, and bind the listbox to it. Then it'll remember all the selections you make. Quite quick to set up, but the downside is that using MV fields is a real pain if you need to extract values out of there for use elsewhere.

                    Comment

                    • TravelingCat
                      New Member
                      • Feb 2010
                      • 82

                      #11
                      TheSmileyOne, thanks a lot for the code. My values are stored it a table anyway, but i did use the ".Selected" property, it does paint the line as chosen, for some reason i didn't find it earlier ... now i have another problem, but i'll try to figure it out.
                      Megalog, i'm using Access 2003

                      Comment

                      Working...