OpenArgs and New Records, within the same form

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • James Bowyer
    New Member
    • Nov 2010
    • 94

    OpenArgs and New Records, within the same form

    Is there any way to create a new record (using the following code or similair)

    Code:
    DoCmd.GoToRecord , , acNewRec
    But have it carry through certain values from the previous form you have created?

    The reson being, I want to be able to create a lot of site records at once, all with the same customer, and same project name. Obviously, getting the customer and project name, etc from the main project setup screen is no problem as you just use openArgs, but I cant see a way of doing it once in the form, and obviusly when you click new, it loses all the openargs.

    Any ideas anyone?
  • TheSmileyCoder
    Recognized Expert Moderator Top Contributor
    • Dec 2009
    • 2322

    #2
    I will admit, that I haven't actually tested it, but I don't THINK that the form looses the OpenArgs when you create a new record. So you should be able to just add code beneath the line you allready have:
    Code:
    DoCmd.GoToRecord , , acNewRec 
    If Me.OpenArgs & ""<>"" then
      'Assuming you have only passed 1 value, the project ID
      Me.tb_ProjectID=clng(Me.OpenArgs)
    End If

    Comment

    • James Bowyer
      New Member
      • Nov 2010
      • 94

      #3
      Presumably if I were to want more than one openargs, I could split it using:
      Code:
      Dim StrOpenArgs as string
      StrOpenargs = Split(Me.Openargs, ";")
      Me.tb_ProjectID=clng(StrOpenArgs(0))
      and so on... Or would that not work?

      Comment

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

        #4
        If you get the syntax correct :P
        Code:
        Dim StrOpenArgs() as string
        Notice the added (), to indicate its an array.

        What I personally use/prefer is to encode my strings in an xml like format.
        Lets presume I want to pass on information to a form, saying its type and ID.
        I would do like This:
        Code:
        dim strArgs as string
        strArgs="<Type>" & me.Tb_Type & "</Type><ID> & me.Tb_ID & "</ID>"
        docmd.OpenForm "frm_Example",,,,,,strArgs

        Then I have code in a module:
        Code:
        Public Function getXML(strXML As String, strElement As String) As String
        On Error GoTo ErrHandler
            If InStr(1, strXML, "<" & strElement & ">", vbTextCompare) > 0 And InStr(1, strXML, "</" & strElement & ">", vbTextCompare) > 0 Then
                'Found it
                Dim intLeft As Integer
                Dim intRight As Integer
                intLeft = InStr(1, strXML, "<" & strElement & ">", vbTextCompare) + Len(strElement) + 2
                intRight = InStr(1, strXML, "</" & strElement & ">", vbTextCompare)
                getXML = Mid(strXML, intLeft, intRight - intLeft)
                Else
                
                GoTo badXML
            End If
            
        Exit Function
        
        badXML:
            MsgBox "An error occured while trying to retrieve XML information,probably due to bad tags" & vbNewLine & "Please contact TheSmileyOne"
            Exit Function
            
        ErrHandler:
            MsgBox "An error occured while trying to retrieve XML information" & vbNewLine & "Please contact TheSmileyOne" & vbNewLine & "Error Number:" & Err.Number & vbNewLine & "Error Description:" & Err.Description, vbOKOnly + vbCritical
            Err.Clear
            Exit Function
        End Function
        So in the form you opened you could get the information from the OpenArgs like this:
        Code:
        Dim strType as string
        dim lngID as long
        strType=getxml(me.OpenArgs,"Type")
        lngID=clng(getxml(me.openargs,"ID"))
        Why do I do it this way? Well if you only have 1 or 2 arguments to pass in the openArgs its not that complicated, but sometimes I might have 4 or 5, and then I simply find it easier to debug, when viewing a Openargs string that also indicates what each value represents.

        Comment

        • James Bowyer
          New Member
          • Nov 2010
          • 94

          #5
          Doh! I always forget The () on the end until I get an error...

          I didn't realise OpenArgs stays in the memory, so its a lot easier than I originally thought.

          Ta very much!!

          Comment

          • NeoPa
            Recognized Expert Moderator MVP
            • Oct 2006
            • 32662

            #6
            James, an important point to remember when perpetuating data across multiple records, or even just perpetuating any data into a new record, is to use the .DefaultValue property, rather than the .Value property. They can both be made to work, it's true, but you get far fewer problems and issues when done using the .DefaultValue property.

            PS. Smiley, I like your thinking about the XML thing. I tend to use Split()/Join() array structures myself, but I might look into the XML thing a little more deeply now you've posted this.
            Last edited by NeoPa; Jan 25 '11, 02:45 PM.

            Comment

            Working...