Wrox Programmer Forums
|
Excel VBA Discuss using VBA for Excel programming.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Excel VBA section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 25th, 2004, 09:54 AM
Registered User
 
Join Date: Nov 2004
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default INSERT INTO QUERY

Hi there,

I am using INSERT INTO query in order to add records to the access db using VBA. My code is as follows:

rivate Sub cmd_OK_Click()
    Dim wrkJet As DAO.Workspace
    Dim db As DAO.Database
    Dim dbname As String
    Dim rs As DAO.Recordset
    Dim strSQL As String
    Dim Q As DAO.QueryDef

    dbname = "C:\Documents and Settings\sanju\Desktop\Project\Commcare.mdb"
    Set wrkJet = CreateWorkspace("", "admin", "", dbUseJet)
    Set db = wrkJet.OpenDatabase(dbname, , True, "Microsoft.Jet.OLEDB.3.51")
    strSQL = "INSERT INTO MasterAccountDetails VALUES (txt_AccName.text, txt_Desc.text, CB_Period.Value, txt_Template.text, txt_Midstarttime.text, txt_Midendtime.text );"
    Set Q = db.CreateQueryDef("")
    Q.Sql = strSQL
    Q.Execute
    Q.Close
    db.Close
    wrkJet.Close

But unfortunately my code generates a run time error stating "Too few parameters.Expected 6."
Does anyone know how to solve this prob?
Thanks a lot for u r help in advance.
Sanjna...

 
Old November 25th, 2004, 10:33 AM
Friend of Wrox
 
Join Date: May 2004
Posts: 642
Thanks: 0
Thanked 43 Times in 42 Posts
Default

It seems you are inserting less values in the table.

If you want to insert less columns in the table then use the following syntax..

insert into tablename (column_1,column_2) values ('value_1','value_2')

Om Prakash
 
Old November 25th, 2004, 11:18 AM
Registered User
 
Join Date: Nov 2004
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default

That's the problem. I am inserting values for all the fields in the table. But i am getting the same error msg.


 
Old November 25th, 2004, 11:39 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 150
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Do you have an autonumber field in your table? If so, use the procedure from Om Prakash
 
Old November 25th, 2004, 12:00 PM
Registered User
 
Join Date: Nov 2004
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I don't have auto number field in my table.

 
Old November 26th, 2004, 12:41 AM
Friend of Wrox
 
Join Date: May 2004
Posts: 642
Thanks: 0
Thanked 43 Times in 42 Posts
Default

Please check the data is coming in the fields:

txt_AccName.text,
txt_Desc.text,
CB_Period.Value,
txt_Template.text,
txt_Midstarttime.text,
txt_Midendtime.text

Other reason could be:
One of the column names specified in a select statement does not exist in the table being queried.


Om Prakash





Similar Threads
Thread Thread Starter Forum Replies Last Post
Insert Into Query. rupen Access VBA 5 July 30th, 2007 09:58 AM
Where does insert query go? bph Access VBA 5 June 12th, 2007 12:26 PM
New Insert Query Neal SQL Server 2000 3 May 8th, 2007 01:22 PM
INSERT Query Neal SQL Server 2000 2 May 3rd, 2007 05:18 PM
I solved insert query.now see this Update Query. [email protected] VB.NET 2002/2003 Basics 2 September 21st, 2006 12:48 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.