?
Solved

Using SQL's Insert command with VB6 and an Access 97 database

Posted on 2003-02-28
13
Medium Priority
?
495 Views
Last Modified: 2013-12-25
Hey all, first time user. OK, the question. :)

Private Sub add_customer_Click()
Dim ItemCode As String
        ItemCode = item_code_box.Text
        Dim CustName As String
        CustName = item_code_box.Text
        Dim CustPhone As String
        CustPhone = item_code_box.Text
        Data1.RecordSource = "INSERT INTO PhoneList (PhoneNo, ProdItemCode, CustomerName) VALUES ('" & ItemCode & "', '" & CustName & "', '" & CustPhone & "')"
        MsgBox "Customer added to database."
End Sub

It isn't adding the information in the text boxes to the database (the table = PhoneList). Where am I going wrong?

---Overlord
0
Comment
Question by:Overlord44
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 6
13 Comments
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 8042919
You need to execute the command (setting the RecordSource is not enough). To execute it, you must Refresh (data1.refresh).

You should consider not to use the data control to do action queries (insert, update, delete). In ADO, you can use Connection.Execute
0
 

Author Comment

by:Overlord44
ID: 8043003
I added data1.refresh in straight after the Data1.RecordSource = blah blah blah line. Now it's given a runtime error 3129-invalid operation, and highlighted the data1.refresh in yellow...
0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 8043025
In this case create a command to run it. Are you using DAO or ADO?
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:Overlord44
ID: 8043055
"Create a command"? How? I don't know which of those I'm using... does "Microsoft Jet Engine" help?

Heh, I'm a newbie at this... :)
0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 8043167
why don't you use the AddNew method:

with data1
   .recorsource="select * from phonelist"
   .refresh
   .addnew
   .fields("PhoneNo").value = CustPhone
   .fields("ProdItemCode").value = ItemCode
   .fields("CustomerName").value = CustName
   .update
end with
0
 

Author Comment

by:Overlord44
ID: 8043226
Where do I put that in? And do I put it in exactly as shown, or with data1 before all the dots?
0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 8043586
>>Where do I put that in?

in your add_customer_Click event.


>>And do I put it in exactly as shown, or with data1 before all the dots?

Exactly as is. When you use the With statement, you don't need to repeat data1 on each line.
0
 
LVL 2

Expert Comment

by:i014354
ID: 8043634
Use ADO instead:

Dim db As adodb.Connection
Dim cmd As adodb.Command
Dim strSQL as string

strSQL = "INSERT INTO PhoneList (PhoneNo, ProdItemCode, CustomerName) VALUES ('" & ItemCode & "', '" & CustName & "', '" & CustPhone & "')"


    Set db = New adodb.Connection
    Set cmd = New adodb.Command

'Open connection using ODBC Datasource called AccessDB
  db.Open "PROVIDER=MSDASQL;dsn=AccessDB;uid=;pwd=;"

 db.BeginTrans
    cmd.ActiveConnection = db
    cmd.CommandType = adCmdText
    cmd.CommandText = strSQL

    cmd.Execute
   
 db.CommitTrans
0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 8043652
Why would you ask someone to add ADO in reference (7.5 meg) to someone that is using DAO? Also, you will have problem refreshing. Updates will not appear as quick if you mic DAO and ADO.
0
 

Author Comment

by:Overlord44
ID: 8043827
Method or Data not recognised for ".AddNew"...
0
 
LVL 70

Accepted Solution

by:
Éric Moreau earned 200 total points
ID: 8043926
I forgot the Recordset object.

with data1
  .recorsource="select * from phonelist"
  .refresh
  with .recordset
    .addnew
    .fields("PhoneNo").value = CustPhone
    .fields("ProdItemCode").value = ItemCode
    .fields("CustomerName").value = CustName
    .update
  end with
end with
0
 

Author Comment

by:Overlord44
ID: 8044087
YES! It works! Thankyou!
0
 

Author Comment

by:Overlord44
ID: 8044089
Thank you very much!!! I'll be using this place again. :)
0

Featured Post

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question