Share via

Can I use a VB function in a SQL statement?

Anonymous
2022-10-26T17:15:13+00:00

Is it possible to use a VB function in a SQL statement?  The first screen shot demonstrates that my function works OK in the VB environment.  The second shows the error I get when I put it in a SQL statement.  This caused me some grief because the error doesn’t display when the SQL is executed inside a VB script.

If I can’t achieve my goal this way, does someone have a suggestion as to how I can select rows where [score_date] is within a week where we know the start date of that week?

Microsoft 365 and Office | Access | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

9 answers

Sort by: Most helpful
  1. Anonymous
    2022-10-26T19:52:52+00:00

    Here's what I get.

    Was this answer helpful?

    0 comments No comments
  2. ScottGem 68,830 Reputation points Volunteer Moderator
    2022-10-26T19:37:58+00:00

    Try this:

    Select *, fncWeekStartDate(score_date) AS WeekStart

    FROM tblScores

    WHERE WeekStart = !0/24/22;

    Was this answer helpful?

    0 comments No comments
  3. Anonymous
    2022-10-26T18:47:13+00:00

    Here's my version of Access.

    Was this answer helpful?

    0 comments No comments
  4. Anonymous
    2022-10-26T18:28:00+00:00

    Function fncWeekStartDate(dtDate As Date) As Date

    Dim ls_temp As String

    On Error GoTo Err_WeekStartDate

    fncWeekStartDate = DatePart("ww", dtDate, vbMonday)

    Dim intDelta

    intDelta = (DatePart("w", dtDate, vbMonday) - 1) * -1

    fncWeekStartDate = DateAdd("d", intDelta, dtDate)

    Exit_WeekStartDate:

    Exit Function 
    

    Err_WeekStartDate:

    ls\_temp = "fncWeekStartDate: " & Err.Description 
    
    MsgBox ls\_temp 
    
    Resume Exit\_WeekStartDate 
    

    End Function

    Was this answer helpful?

    0 comments No comments
  5. Anonymous
    2022-10-26T18:02:37+00:00

    Should be ok, if it's a Public function. Could you post (at least the first few lines of) the VBA code? What version of Access are you running?

    Was this answer helpful?

    0 comments No comments