Results 1 to 5 of 5
  1. #1
    Join Date
    Jul 2003
    Location
    Portugal
    Posts
    111

    Unanswered: How to create a function with this elements?

    As I have to repeat times and times this lines, I thinked that the best way would be to create a function. But... I don't know how...

    Can someone give an idea?

    -----
    Dim SearchString, SearchChar, MyPos
    Dim pat As String
    SearchString = Forms!Proc!NDossier
    SearchChar = "/"
    MyPos = InStr(1, SearchString, SearchChar, vbTextCompare)
    Dim MyString
    MyString = Forms!Proc!NDossier
    Mid(MyString, MyPos, 1) = "-"
    pat = Application.CurrentProject.Path & "\" & MyString & "\"
    ------

    I need the value from "pat" in each form.

    Thank you for your sugestion.

    Ruas

  2. #2
    Join Date
    Nov 2003
    Posts
    1,487

    Re: How to create a function with this elements?

    Try this:

    Code:
    Private Function GetPath(MyString As String) As String
        Dim SearchChar, MyPos
        If IsNull(MyString) Or MyString = "" then Exit Function
        SearchChar = "/"
        MyPos = InStr(1, MyString, SearchChar, vbTextCompare)
        Mid(MyString, MyPos, 1) = "-"
        GetPath = Application.CurrentProject.Path & "\" & MyString & "\"
    End Function
    And this is how you might use it:

    Code:
    Dim myStringVariable As String
    myStringVariable = GetPath(Forms!Proc!NDossier)
    Of course, based on the code you supplied, the function I have provided here is a Private function and therefore can only be called from the module it resides in. If you want the capability to call this function from anywhere in your project, then place the code into a database module and change the Private to Public.

    Hope this helps

  3. #3
    Join Date
    Jul 2003
    Location
    Portugal
    Posts
    111
    Thank you.

    I used your help as a Public Function and it works great!

    Me and my 16 forms are very happy with your help.

    Thank you again.

    Ruas

  4. #4
    Join Date
    Nov 2003
    Posts
    1,487
    You are very welcome...and if you like...you can take the function a step further and make the SearchChar string part of the functions parameters. This way you can change the SearchChar string on-the-fly so to speak:

    Code:
    Private Function GetPath(MyString As String, Optional SearchChar as string) As String
        Dim SearchChar, MyPos
        If IsNull(MyString) Or MyString = "" then Exit Function
        If IsNull(SearchChar) Or SearchChar = "" then SearchChar = "/"
        MyPos = InStr(1, MyString, SearchChar, vbTextCompare)
        Mid(MyString, MyPos, 1) = "-"
        GetPath = Application.CurrentProject.Path & "\" & MyString & "\"
    End Function
    In this function sample, the SearchChar string can be optionaly provided as a parameter within the function call so that you can use whatever SearchChar you want. If a SearchChar is not provided in the function call, then the default "/" string is used.

    You can then use the function either way (use default SearchChar):
    Code:
    Dim myStringVariable As String
    myStringVariable = GetPath(Forms!Proc!NDossier)
    or..supply the SearchChar ("/" can be anything) :

    Code:
    Dim myStringVariable As String
    myStringVariable = GetPath(Forms!Proc!NDossier, "/")
    It may really serve no purpose here but i's just a little extra to show you what can be done with a function

  5. #5
    Join Date
    Jul 2003
    Location
    Portugal
    Posts
    111

    Talking

    I'm amazing with the quality of the help we can get here and specialy with people like you...

    Thank you again.

    Ruas

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •