Changing form defaults based on form values...

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Luongo
    New Member
    • Oct 2006
    • 22

    Changing form defaults based on form values...

    Hi, I'm a beginner to Access so this question may seem pretty simple to some of you, but I could not seem to find an answer elsewhere on the web. I have a form which allows the user to select two variables - shift number and supervisor. When this form is submitted it opens another form, in which I would like the default values of fields 'Shift' and 'Supervisor' to be the variables entered in the original form. Therefore, the default values would be changed based on what the user selects. Could someone explain how to do this?

    Thanks....
  • Tanis
    New Member
    • Mar 2006
    • 143

    #2
    I would use the OpenArgs method. There is a very good example of passing parameters on this site.

    http://fmsinc.com/free/newtips/access/accesstip13.asp

    Comment

    • Luongo
      New Member
      • Oct 2006
      • 22

      #3
      Hmm... thanks Tanis, but I seem to be getting an error using the code provided there. For some reason the debugger does not like this line:

      If Me.Len(OpenArgs ) > 0

      It highlights the ".Len" as an error. Not sure what that's all about, as I inserted the OpenArgs function as instructed.....

      Comment

      • Tanis
        New Member
        • Mar 2006
        • 143

        #4
        Should be

        If Len(Me.OpenArgs ) > 0

        Comment

        • NeoPa
          Recognized Expert Moderator MVP
          • Oct 2006
          • 32656

          #5
          Try
          Code:
          If Len(Me.OpenArgs) > 0 Then

          Comment

          • Luongo
            New Member
            • Oct 2006
            • 22

            #6
            Thanks again, it's working now (sorry, I forgot to include the 'Then' in my earlier paste).

            Just one more question about this if I can... How can I pass multiple variables using this OpenArgs function? As mentioned, I have 'Shift' and 'Supervisor' values that I'd like to pass to the form. However, the example seems to only deal with one. Is there a way to expand the OpenArgs to include both values?

            Comment

            • NeoPa
              Recognized Expert Moderator MVP
              • Oct 2006
              • 32656

              #7
              You could assign a string with values separated by your own designated separator character. I typically use a '|' or a ';' as I want to make it unlikely that any of the data contains the character normally.
              In the OnOpen event of the called form, you can handle extracting the values you want from the string passed.

              I have to admit that I was unaware of this feature so, many thanks to Tanis for bringing it to my attention.

              Comment

              • Luongo
                New Member
                • Oct 2006
                • 22

                #8
                Yes, thanks to both of you for all your help. I've noticed that the current code just sets the default value for the one record, and not on any subsequent records. Since I'm in datasheet mode, I'd like each new row to have that default value. I tried inserting this code in the OnLoad event of the form (within the code provided above), but it did not seem to work:

                Me.Supervisor.D efaultValue = strValue

                Here's the whole thing:


                Code:
                 Private Sub Form_Load()
                
                      Dim intPos As Integer 'Position of the Pipe
                      Dim strControlName As String 'Controlname passed
                      Dim strValue As String 'Value to assign
                
                      If Len(Me.OpenArgs) > 0 Then
                
                        intPos = InStr(Me.OpenArgs, "|")
                
                        If intPos > 0 Then
                
                          'Retrieve Control Name
                          strControlName = Left$(Me.OpenArgs, intPos - 1)
                
                          'Retrieve Value to Assign
                          strValue = Mid$(Me.OpenArgs, intPos + 1)
                
                            'Make the Assignment
                
                          Me.Supervisor.DefaultValue = strValue
                
                        End If
                
                      End If
                      
                   
                    
                    End Sub

                Comment

                Working...