How to pass a string field as a parameter

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sueb
    Contributor
    • Apr 2010
    • 379

    #1

    How to pass a string field as a parameter

    I'm trying to tighten up the code in my database, so there is a function that I've made public. However, I don't have the syntax right for passing in the field that contains the parameter data.

    Here's the public function:

    Code:
    Function Open_eChart(strChartNum As String)
    On Error GoTo Err_Open_eChart
    
        Dim retVal As Variant
        Dim strPartialPath As String
        Const conPAth As String = "V:\IUR\eCharts\"
    
        If IsNull(strChartNum) Then Exit Function
    
        strPartialPath = Replace(Replace(strChartNum, "\", ""), ",", "_")
    
        If Dir$(conPAth & strPartialPath, vbDirectory) = "" Then MkDir (conPAth & strPartialPath)
    
        retVal = Shell("Explorer.exe " & conPAth & strPartialPath, vbNormalFocus)
    
    Exit_Open_eChart:
       Exit Function
    
    Err_Open_eChart:
        MsgBox Err.Description
        Resume Exit_Open_eChart
    
    End Function
    and here's how I'm calling it:

    Code:
    Private Sub Open_eChart_Click()
        'Open_eChart Me![ChartNum]
        Open_eChart (Me![ChartNum])
    End Sub
    Both the lines in the call give the same error, which is a compile error saying that this is an "invalid use of property", and pointing to the "ChartNum" field.

    (Also, do you think I need the error handling in the public function? It seems pretty sturdy on its own, but I don't know.)
  • beacon
    Contributor
    • Aug 2007
    • 579

    #2
    To answer your last question first, I would remove the error handling until you get the function to work. Then you can test it with the error handling to ensure that everything works the way you were intending. To answer your second to last question next, personally, I would use the following code:

    Code:
    Private Sub Open_eChart_Click()  
        Open_eChart (Me.ChartNum) 
    End Sub
    Try this first and if it works, then you can pretty much disregard the rest of the thread.

    Now for the questions (assuming it still doesn't work)...

    Have you done any debugging to see what kind of value is getting passed into the function?

    I would slap a messagebox before your line #8 in your first block of code and one after it, then re-run the code.

    I know this sounds elementary, and for that I apologize in advance, but have you double-checked to ensure that [ChartNum] is a Text field? I've created many a database for patients with ID numbers that sometimes I needed to be numbers and other times I needed to be strings, so to see you call your field [ChartNum] instead of [strChartNum] or [txtChartNum] made me want to make sure you double checked it.

    Another thing you can try is to create a local string variable in the function and assign the argument to the variable, then test that out. For instance:

    Code:
    Function Open_eChart(strChartNum As String) 
    On Error GoTo Err_Open_eChart 
        
        Dim strTemp as String
        Dim retVal As Variant 
        Dim strPartialPath As String 
        Const conPAth As String = "V:\IUR\eCharts\" 
       
        strTemp = strChartNum
    
        If IsNull(strTemp) Then Exit Function

    Comment

    • ADezii
      Recognized Expert Expert
      • Apr 2006
      • 8834

      #3
      1. Both the Name of your Command Button and Public Function share the exact same Name, namely Open_eChart. This is a No-No. Change the Name of the Function to Open_eChart_2.
      2. Since the Function is not returning a Value, I prefer the following Syntax to Execute the Function:
        Code:
        Call Open_eChart_2(Me![ChartNum])

      Comment

      • beacon
        Contributor
        • Aug 2007
        • 579

        #4
        Good eye ADezii

        Comment

        • ADezii
          Recognized Expert Expert
          • Apr 2006
          • 8834

          #5
          Thanks beacon, at least something still works!(LOL).

          Comment

          • sueb
            Contributor
            • Apr 2010
            • 379

            #6
            Okay, the string-passing works great, guys! But it seems that line #12 (in my original post), intended to make a directory if one doesn't already exist, doesn't work; instead, I get a pop-up error "Path not found". What am I doing wrong there? I actually thought this had been working, but apparently not.

            Edit: Never mind, it is working. Not sure why it seemed not to be, but it's all good now--thanks, again!

            Comment

            • beacon
              Contributor
              • Aug 2007
              • 579

              #7
              If starts not working again, add some breakpoints at that line in your code so you can see what the values are that comprise your path. That way, if something is wrong (maybe has a character you didn't intend on it having) you'll be able to see it and can add additional code rectify the situation from occurring again.

              Comment

              Working...