473,396 Members | 1,834 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Insert query using values and query

Hello everyone,

I am trying to make an insert query that inserts information using form values and a query that gets the id from the name of a field in the form.

I got a form that shows object name, date completed and department completed.

I am trying to do the following:

Expand|Select|Wrap|Line Numbers
  1. INSERT INTO DB (Person_id, Object_id, Date_completed, Department_completed) Values (Forms!Form1!Person_id, Querydata, Forms!Form2!Date_completed, Forms!Form2!Department_completed);
The query that i need data from selects the ID that belongs to the object_name in the form because this is a key field in the db i want to insert.

I tried many things but none seem to work. Do you guys have an idea how i can accomplish this?

Or is there a way to use the same syntax as forms!Formname!Formfield for queries?

Thanks in advance!
Apr 23 '11 #1

✓ answered by ADezii

Here is the Logic as to how this can be accomplished, make your own substitutions where necessary. Certain assumptions were made concerning Data Types, Field Names, etc.:
Expand|Select|Wrap|Line Numbers
  1. Dim strSQL As String
  2. Dim lngPersonID As Long
  3. Dim dteDateCompleted As Date
  4. Dim strDeptCompleted As String
  5. Dim lngID As Long
  6.  
  7. 'Variable Assignments taken from Form (No Validation, No Data Type Checking!)
  8. lngPersonID = Me![Person_id]
  9. dteDateCompleted = Me![Date_completed]
  10. strDeptCompleted = Me![Department_completed]
  11.  
  12. lngID = DLookup("[ID]", "QueryData", "[Object Name]='" & Me![Object_Name] & "'")
  13.  
  14. strSQL = "INSERT INTO DB (Person_id, Object_id, Date_completed, Department_completed) Values (" & _
  15.           lngPersonID & "," & lngID & ",#" & dteDateCompleted & "#,'" & strDeptCompleted & "');"
  16.  
  17. CurrentDb.Execute strSQL, dbFailOnError

1 2044
ADezii
8,834 Expert 8TB
Here is the Logic as to how this can be accomplished, make your own substitutions where necessary. Certain assumptions were made concerning Data Types, Field Names, etc.:
Expand|Select|Wrap|Line Numbers
  1. Dim strSQL As String
  2. Dim lngPersonID As Long
  3. Dim dteDateCompleted As Date
  4. Dim strDeptCompleted As String
  5. Dim lngID As Long
  6.  
  7. 'Variable Assignments taken from Form (No Validation, No Data Type Checking!)
  8. lngPersonID = Me![Person_id]
  9. dteDateCompleted = Me![Date_completed]
  10. strDeptCompleted = Me![Department_completed]
  11.  
  12. lngID = DLookup("[ID]", "QueryData", "[Object Name]='" & Me![Object_Name] & "'")
  13.  
  14. strSQL = "INSERT INTO DB (Person_id, Object_id, Date_completed, Department_completed) Values (" & _
  15.           lngPersonID & "," & lngID & ",#" & dteDateCompleted & "#,'" & strDeptCompleted & "');"
  16.  
  17. CurrentDb.Execute strSQL, dbFailOnError
Apr 24 '11 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Steve | last post by:
Hi, Have a database that contains various tables. I need to run a query on two tables. Table A contains a column called Titles Table B contains a column called Uni_Titles
0
by: Paul | last post by:
I have the query "SELECT * FROM RIGHT JOIN VisiterInfo ON .=VisiterInfo." which I use to select data from two tables to display on screen. I use an Access file with an OleDb connection. I need...
20
by: talktozee | last post by:
Hey, everyone! Basically, I need to insert *multiple rows* into table A from table B based upon some criteria, and I need to insert some static values along with each row from table A. For...
8
by: Roland Hall | last post by:
In Access you use "*" + + "*", + can be replaced with & Calling a parameterized query in Access requires % be used in place of *, however, all that I have read show dynamic SQL passed to Access: ...
3
by: tushar jadhav | last post by:
Suppose, i hv an 10 columns and next to that other columns in a DB Table. That 10 columns hv same default values & others are retriving from other table and are nearly 1000 records. How can i insert...
0
by: fitzini | last post by:
Hi all, I need to update multiple rows using a sub query. I've pasted sample code below to hopefully help recreate what I'm trying to do. drop table dummy_test create table dummy_test (cmr...
7
by: dongletran06 | last post by:
Hi, Please help me find out what wrong with my codes in inputting from my form to mysql database using drop down menu. Below is the codes I used. Only the drop down is not working but the "input...
4
by: TechnoAtif | last post by:
Hi ALL I have entered some array values using checkboxes into mysql database through a form. Next iam creating a searchpage where all those cateogories inserted through checkboxes has to be...
2
by: paulmitchell507 | last post by:
I think I am attempting a simple procedure but I just can't figure out the correct syntax. My asp (classic) page runs a SELECT query to obtain dates and ID's from 2 tables uSQL = "SELECT...
2
by: puneetmca | last post by:
hi i want to insert employees DOB into database through html forms but my code is not working....only empty record is shown in the database in edob ..rest of the records are working...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.