Solved

Adding New Records Events in Access and SQL Query

Posted on 2004-09-14
7
250 Views
Last Modified: 2006-11-17
Hi Every Body
I have a question on Forms.
I am using Access in order to make my Forms and I am using SQL server for DataBase.
Here is the Scenario:
Basically I want to know what event happens when one uses the Add New Button on the Navigation Bar (the Bar on the Button of Every form which let’s you to go back and forth and Add a new data to your Table)?
I have a form, which we Call it F1 and it’s Record Source is Q1 Query, I know how to send some Data from it to another Table Table1 using a Command Button and Some VB Coding in it’s OnClick Event, But I want to be able to send the same Data when I want to add a new data to my F1 Form, without using my Button. Any suggestion?

Best Regards
Sanan

Also on the same Note the Data that I want to send is the Data that comes from a subform “SF1” on my F1 Form, Rather than Pick them one by one, I want to run a Following SQL query on this SUBForm “SF1” :
SELECT     OrderID, ProductID, UnitPrice, Quantity, Discount
FROM         [Order Details]
WHERE     (OrderID =         )
On the Where condition I want to say that Basically the OrderID is the same as the Original F1 Order ID.
How can I do that?
And After it is done Append the Result Row or Rows to my table1 Table.
How can I do it?

Best regards
Sanan
0
Comment
Question by:Bijanbijan
  • 4
  • 3
7 Comments
 
LVL 77

Expert Comment

by:peter57r
ID: 12053749
Hello Bijanbijan,

The addnew button just positions you into an empty record it doesn't save a new (even empty) record.  The record is not saved until you move away from it or otherwise do a save record action provided you have enetred some data.
The only event that will fire for the new record is the form_current event, although other events may fire to complete the processing of the record that was on the screen before you clicked the add new button.

Are you saying that you want to save the record that you are about to enter into 2 different tables?
This does not sound like good design, on the face of it.

Pete
0
 

Author Comment

by:Bijanbijan
ID: 12061662
Hi Pete
Thanks for reply.
What I want to do is simple; basically As I explained Before,
I have a form, which we Call it F1 and it’s Record Source is Q1 Query, I know how to send some Data from it to another (not related) Table Table1 using a Command Button and Some VB Coding in it’s OnClick Event, Her is the new Scenario, Also by pushing the same Button I want to be able to do what ever the Navigation Button does (add new data Button Off Course), which is Updatting my Original Q1 query (and Tables) and Moving to a New Record.
Or I can use the add new Button of Navigation Bar, to do what it supposed to do, also on top of it to add the all or some of data to my Extra Table (Table1).
Either of the above procedures would do it for me.
Back to your reply, are you telling me that this is not a good design, would you please explain why?

Best regards
Bijan
0
 
LVL 77

Expert Comment

by:peter57r
ID: 12066768
So, as I understand it,  you have a bound form.
On the form you have a button which writes a record to a different table to the one the form is bound to - is that right?

You now want to save the the current record to its bound source and move to a new record- is that right?

If so then modify your code that writes to the second table to include:

Me.dirty = false
put this line before you write to the other table.
It will save the current record to the bound source.

After the code to write the other table put:
docmd.GoToRecord ,,acNewRec

This assumes that the code is in the form module and not in a standard module, otherwise you need
docmd.GoToRecord acDataForm,"formname",acNewRec.

Obviousy I do not know anything about your application, but itis unusual to add the same data to two different tables.
It is inevitable that at some point the tables get out of line, because of user actions or program bugs.

But you may have good reasons for doing this.

Pete
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 

Author Comment

by:Bijanbijan
ID: 12072659
Hi Peter57r
I sort of figuredout the first part of the question, Could youb help me on secon part of the Question, I sort of figure that one also, I just have a little problem with it which I will present it to you.
I want to send some Data from an access Form to a SQL Database Table (not related Table)
I have the following Codes which Works:

Private Sub Command95_Click()
 Dim myCn As New ADODB.Connection
 Dim myRs As New ADODB.Recordset
  myCn.ConnectionString = "DSN=TestE"
 myCn.Open
 myRs.Open "INSERT INTO TABLE1  VALUES (" & OrderID.Value & "," & OrderID.Value + 10000 & ",'" & CustomerID.Value & "','" & CompanyName.Value & "','" & OrderDate.Value & "'," & Tax1.Value & "," & Tax2.Value & "," & Total.Value & ");", myCn, adOpenDynamic
 End Sub

But the Following Does not work:

Private Sub Command95_Click()
Dim myCn As New ADODB.Connection
 Dim myRs As New ADODB.Recordset
 myCn.ConnectionString = "DSN=TestE"
 myCn.Open
myRs.Open "INSERT INTO [dbo.OrderDetailsQoute] SELECT FROM [view.Order Details Extended] WHERE (OrderID=10036)", myCn, adOpenDynamic
 End Sub

Off Course the actual WHERE Condition is
WHERE (OrdrerID=Forms!Qoute!OrderID.Value)

Could some one tell me, why? (I think it should be in Punctuation Error in Insert Line.)

Best Regards
Bijan
0
 

Author Comment

by:Bijanbijan
ID: 12081702
Hi Peter
How are you?
I am stillw aiting for you reply on my last question.
Best Regards
Bijan
0
 
LVL 77

Accepted Solution

by:
peter57r earned 125 total points
ID: 12086258
I guess you mean:
SELECT * FROM [view.Order Details Extended]

and to use a form reference you need:

 "INSERT INTO [dbo.OrderDetailsQoute] SELECT * FROM [view.Order Details Extended] WHERE (OrderID= " & Forms!Qoute!OrderID.Value & ")",

Pete
0
 

Author Comment

by:Bijanbijan
ID: 12102635
Hi Pete
I am sorry for delay.
But Thanks so much It worked just Fine.
Best regards
Bijan
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Database tuning – How to start and what to tune. This question is frequently asked by many people, both online and offline. There is no hard and fast rule-of-thumb for performance tuning, however, before beginning the tuning process one should a…
This article describes some very basic things about SQL Server filegroups.
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

762 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now