Results 1 to 9 of 9

Thread: USer-Defined Function in query

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2000
    Location
    Brooklyn NY USA
    Posts
    1,258

    USer-Defined Function in query

    Is there a way to creat user defined function for a access database when calling the datbase from vb


    For example

    VB Code:
    1. Sub OpenRecordSet()
    2.  
    3. Set rst = dbs.OpenRecordSet("Select Field1, MyFunction(Field1) From Table1")
    4.  
    5. End Sub
    6.  
    7. Function MyFunction(AnyThing)
    8. MyFunction = Anything + 1
    9. End Function

  2. #2
    Addicted Member
    Join Date
    May 2004
    Location
    China
    Posts
    228
    You above example would work if you changed it to

    VB Code:
    1. Set rst = dbs.OpenRecordSet("Select Field1," & MyFunction(Field1) & "From Table1")

    Although I am not sure that is a valid SQL statement. What exactly do you want to do?
    You know the best way to find the answer to your question? SEARCH!
    www.google.com
    www.planet-source-code.com
    www.msdn.com
    http://www.vbforums.com/search.php?s=
    P.S. Its faster than waiting for replies too!

    [vbcode]
    If InStr(1, Message, "FIX MY CODE") <> 0 Then Reply = False
    If AnswerIn(Google, VB_Forums, PSC, MSDN) = True Then Reply = False
    [/vbcode]

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2000
    Location
    Brooklyn NY USA
    Posts
    1,258
    Isn't there anyone here who understands the question?

  4. #4
    Banned jhermiz's Avatar
    Join Date
    Jun 2002
    Location
    Antarctica
    Posts
    2,492
    Originally posted by shragel
    Isn't there anyone here who understands the question?
    No we are all stupid..

    You cannot do that.

  5. #5
    Banned jhermiz's Avatar
    Join Date
    Jun 2002
    Location
    Antarctica
    Posts
    2,492
    Originally posted by shragel
    Isn't there anyone here who understands the question?
    Why do you even have to do it like that...try this terd dropper:

    VB Code:
    1. public Sub OpenRS()
    2. 'error handling here...
    3.  
    4. dim i as Integer
    5.  
    6. i= Call SomeFunc()
    7.  
    8. db.OpenRecordset("SELECT * FROM Users WHERE IQ > " & i)
    9. 'other code

    No reason to call it within your SQL.

    Jon

  6. #6

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2000
    Location
    Brooklyn NY USA
    Posts
    1,258
    Let Me Explain.

    In Access When you go to Module and Paste the Following Code.

    VB Code:
    1. Function MyFunction(AnyThing)
    2. MyFunction = Anything + 1
    3. End Function

    And then you go in to Query Design and put in sql ciew the following code

    VB Code:
    1. Select FieldNum, MyFunction(FieldNum) From Table1

    You will get two columns. One column will have the value of FieldNum, And the other column will have the value of MyFunction(FieldNum) Which is FieldNum + 1.

    So now the question, is there any way to do this in VB?

  7. #7
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263
    Bill was nuts to think that ACCESS could be a SQL backend - that was before he realized how to really pilfer SYBASE...

    Access with VBA - argghh...

    Why not use real SQL, or at least MSDE?

    The power is so much greater.

  8. #8
    Hyperactive Member
    Join Date
    Sep 2001
    Location
    San Jose, Ca. - USA
    Posts
    302
    vb does not have a direct connection with your mdb file, the way access does. vb uses rdo, dao, or ado for connections to a mdb.

    with that being said, NO you cannot apply vb functions to a field via a SQL statement. There are some built-in SQL functions that are supported like SUM and AVERAGE but these are not vb functions they are SQL functions.

    What you are asking for are only possible in Access and/or via stored procedures in a real DBMS.

    If you open up the Access query builder you can see the available SQL functions.

  9. #9

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2000
    Location
    Brooklyn NY USA
    Posts
    1,258
    Originally posted by frigginjerk
    vb does not have a direct connection with your mdb file, the way access does. vb uses rdo, dao, or ado for connections to a mdb.

    with that being said, NO you cannot apply vb functions to a field via a SQL statement. There are some built-in SQL functions that are supported like SUM and AVERAGE but these are not vb functions they are SQL functions.


    Just for the record, You can use any vb function in your sql query to access. For example InStr() Or Right() Or Shell()

    I Just dont know how to create UDF in VB.

Posting Permissions

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



Click Here to Expand Forum to Full Width