Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
268 views
in Technique[技术] by (71.8m points)

sql - Error 3061: Too few parameters 1 required when using datepart() in VBA

I want to fill out an excel sheet, the data is stored in a database under SQL server. I do this via VBA

Sub fillRows(db As DAO.Database, query As String, row As Integer)

    Dim rs As DAO.recordSet
    Set rs = db.OpenRecordset(query, dbOpenDynaset, dbSeeChanges)

    column = 2
    Do While Not rs.EOF
        ' fill the row

         Sheets(1).Cells(row, column) = rs.Fields(0).Value

         column = column + 1

         ' Move to next Record
         rs.MoveNext
    Loop
    rs.Close
End Sub


Sub main()


    Dim conn As DAO.Database
    Dim query As String

    Set conn = connectToDb()

    query = "select idCourse from dataCourse where datepart(dd,depart_time) = 18 order by idCourse"

    Call fillRows(conn, query, 1)

End Sub

I got this error: Error 3061: Too few parameters 1 required

the problem is indeed the query and more precisely "datepart" because when I changed the request to

select idCourse from dataCourse order by idCourse

it works.

I specify that when I launched the request with datepart() in SQL Server Management Studio it worked.

I think datepart() is a VBA function so that's why there is a conflict.

I tried also query = "select idCourse from traincourse where " & DatePart("d", depart_time) & " = 18 order by idCourse" I see no error but the recordSet is empty !

question from:https://stackoverflow.com/questions/65926786/error-3061-too-few-parameters-1-required-when-using-datepart-in-vba

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

my problem is that my tables are hosted at sql server. and via VBA I use DAO to interoperate the database. therefore the queries that work in Microsoft SQL server management do not necessarily work on access since the access queries are a bit specific


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...