473,857 Members | 1,870 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Update query problem

rrh
I am trying to update a field in one table with data from another
table. The problem I'm running into is I need to base the update on a
range of data in the 2nd table.

Table 1 has:
date field
new field

table 2 has:
key field (autonumber)
date field

I need to populate the "new field" in table 1 with the "key field"
from table 2 where the "date field" in table 2 is => the "date field"
in table 1 but < the "date field" in table one plus one month.

Based on the way the database is used, there should only be 1 record
that would match the criteria described above.

Any help would be greatly appreciated!
Nov 12 '05 #1
3 6319
You could use the following code, which should work ok...

Sub UpdateDateField ()
Dim myRec As DAO.Recordset
Dim fromDate As Date
Dim toDate As Date
Dim myKey As Long

Set myRec = CurrentDb().Ope nRecordset("SEL ECT * FROM [Table 1];")
myRec.MoveFirst
Do Until myRec.EOF
DoEvents
fromDate = myRec![date field]
toDate = DateAdd("m", 1, fromDate)

If DCount("[key field]", "[Table 2]", _
"([date field]>=#" & fromDate & "#) " & _
"AND ([date field]<#" & toDate & "#)") = 1 Then
myKey = DLookup("[key field]", _
"[Table 2]", "([date field]>=#" _
& fromDate & "#) AND ([date field]<#" _
& toDate & "#)")

myRec.Edit
myRec![new field] = myKey
myRec.Update
Else
'Handle exceptions - key not found, or more than
'one record....
End If

myRec.MoveNext
Loop
myRec.Close
Set myRec = Nothing
End Sub

"rrh" <rr*@swbell.net > wrote in message
news:2e******** *************** **@posting.goog le.com...
I am trying to update a field in one table with data from another
table. The problem I'm running into is I need to base the update on a
range of data in the 2nd table.

Table 1 has:
date field
new field

table 2 has:
key field (autonumber)
date field

I need to populate the "new field" in table 1 with the "key field"
from table 2 where the "date field" in table 2 is => the "date field"
in table 1 but < the "date field" in table one plus one month.

Based on the way the database is used, there should only be 1 record
that would match the criteria described above.

Any help would be greatly appreciated!

Nov 12 '05 #2
Ooopsss... If I read your question properly, it seems you want to do this in
a query... In this case, you can create one query, with the fields of 'Table
1' (date field and new field) and the following extra user defined fields;

in one column;
to_date: DateAdd("m",1,[date field])

in another column;
key_field: DLookUp("[key field]","[Table 2]","[date field]>=#" & [date
field] & "# AND [date field]<#" & [to_date] & "#")

Then you could write an update query, to update Table 1, where the 'date
field' of this query matches the 'date field' of Table 1... You will then
update the 'new field' in Table 1, from the key_field in the query....

"Ruskin Hardie" <Ru******@xtra. NOSPAM.co.nz> wrote in message
news:X1******** **************@ news.xtra.co.nz ...
You could use the following code, which should work ok...

Sub UpdateDateField ()
Dim myRec As DAO.Recordset
Dim fromDate As Date
Dim toDate As Date
Dim myKey As Long

Set myRec = CurrentDb().Ope nRecordset("SEL ECT * FROM [Table 1];")
myRec.MoveFirst
Do Until myRec.EOF
DoEvents
fromDate = myRec![date field]
toDate = DateAdd("m", 1, fromDate)

If DCount("[key field]", "[Table 2]", _
"([date field]>=#" & fromDate & "#) " & _
"AND ([date field]<#" & toDate & "#)") = 1 Then
myKey = DLookup("[key field]", _
"[Table 2]", "([date field]>=#" _
& fromDate & "#) AND ([date field]<#" _
& toDate & "#)")

myRec.Edit
myRec![new field] = myKey
myRec.Update
Else
'Handle exceptions - key not found, or more than
'one record....
End If

myRec.MoveNext
Loop
myRec.Close
Set myRec = Nothing
End Sub

"rrh" <rr*@swbell.net > wrote in message
news:2e******** *************** **@posting.goog le.com...
I am trying to update a field in one table with data from another
table. The problem I'm running into is I need to base the update on a
range of data in the 2nd table.

Table 1 has:
date field
new field

table 2 has:
key field (autonumber)
date field

I need to populate the "new field" in table 1 with the "key field"
from table 2 where the "date field" in table 2 is => the "date field"
in table 1 but < the "date field" in table one plus one month.

Based on the way the database is used, there should only be 1 record
that would match the criteria described above.

Any help would be greatly appreciated!


Nov 12 '05 #3
rrh
I think I can use either suggestion. Thanks so much for your help. I
was going in circles.

"Ruskin Hardie" <Ru******@xtra. NOSPAM.co.nz> wrote in message news:<g7******* *************** @news.xtra.co.n z>...
Ooopsss... If I read your question properly, it seems you want to do this in
a query... In this case, you can create one query, with the fields of 'Table
1' (date field and new field) and the following extra user defined fields;

in one column;
to_date: DateAdd("m",1,[date field])

in another column;
key_field: DLookUp("[key field]","[Table 2]","[date field]>=#" & [date
field] & "# AND [date field]<#" & [to_date] & "#")

Then you could write an update query, to update Table 1, where the 'date
field' of this query matches the 'date field' of Table 1... You will then
update the 'new field' in Table 1, from the key_field in the query....

"Ruskin Hardie" <Ru******@xtra. NOSPAM.co.nz> wrote in message
news:X1******** **************@ news.xtra.co.nz ...
You could use the following code, which should work ok...

Sub UpdateDateField ()
Dim myRec As DAO.Recordset
Dim fromDate As Date
Dim toDate As Date
Dim myKey As Long

Set myRec = CurrentDb().Ope nRecordset("SEL ECT * FROM [Table 1];")
myRec.MoveFirst
Do Until myRec.EOF
DoEvents
fromDate = myRec![date field]
toDate = DateAdd("m", 1, fromDate)

If DCount("[key field]", "[Table 2]", _
"([date field]>=#" & fromDate & "#) " & _
"AND ([date field]<#" & toDate & "#)") = 1 Then
myKey = DLookup("[key field]", _
"[Table 2]", "([date field]>=#" _
& fromDate & "#) AND ([date field]<#" _
& toDate & "#)")

myRec.Edit
myRec![new field] = myKey
myRec.Update
Else
'Handle exceptions - key not found, or more than
'one record....
End If

myRec.MoveNext
Loop
myRec.Close
Set myRec = Nothing
End Sub

"rrh" <rr*@swbell.net > wrote in message
news:2e******** *************** **@posting.goog le.com...
I am trying to update a field in one table with data from another
table. The problem I'm running into is I need to base the update on a
range of data in the 2nd table.

Table 1 has:
date field
new field

table 2 has:
key field (autonumber)
date field

I need to populate the "new field" in table 1 with the "key field"
from table 2 where the "date field" in table 2 is => the "date field"
in table 1 but < the "date field" in table one plus one month.

Based on the way the database is used, there should only be 1 record
that would match the criteria described above.

Any help would be greatly appreciated!


Nov 12 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
2228
by: David Shorthouse | last post by:
Hello folks, I have a problem with an update query on an asp not updating the table in an Access db. The code runs, I have no errors, but when I examine the table, nothing was updated. The query works as it should in Access. Could anyone direct me to some sources that describe solutions to what is likely a common problem? The strange thing is, I have another update query for a different db on an asp that works just great. Is there...
17
5038
by: kalamos | last post by:
This statement fails update ded_temp a set a.balance = (select sum(b.ln_amt) from ded_temp b where a.cust_no = b.cust_no and a.ded_type_cd = b.ded_type_cd and a.chk_no = b.chk_no group by cust_no, ded_type_cd, chk_no)
10
3294
by: Randy Harris | last post by:
I imported records into a table, later found out that many of them had trailing spaces in one of the fields. If I'd caught it sooner, I could have trimmed the spaces before the import. This wouldn't work (nothing changed): UPDATE tblManuals SET tblManuals.PARTNUM = Trim(); Would someone please tell me how to do an update query that will trim the spaces?
9
2142
by: James Butler | last post by:
Our setup: Online db: MySQL Inhouse db: MS Access 97 with MySQL tables linked via ODBC Our issue: Almost every field updates successfully, except one. A scenario: Information is written to online db. We run an inhouse query to collect info from online db and update
8
3729
by: Maxi | last post by:
There is a lotto system which picks 21 numbers every day out of 80 numbers. I have a table (name:Lotto) with 22 fields (name:Date,P1,P2....P21) Here is the structure and sample data: "Date","P1","P2","P3","P4","P5","P6","P7","P8","P9","P10","P11","P12","P13","P14","P15","P16","P17","P18","P19","P20","P21" 1/1/2005,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21 1/2/2005,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22
2
5665
by: joeyrhyulz | last post by:
Hi, I'm trying to make a very simple update statement (in Oracle) in jet sql that seems much more difficult than it should be. The root of my problem is that I'm trying to update a field on a table using dmax, which references another query to update the table. Although I have all of the correct keys from the physical table joined to the query in the dmax function, the code/ms access seems to ignore the joins. As a result, all payees...
16
3533
by: ARC | last post by:
Hello all, So I'm knee deep in this import utility program, and am coming up with all sorts of "gotcha's!". 1st off. On a "Find Duplicates Query", does anyone have a good solution for renaming the duplicate records? My thinking was to take the results of the duplicate query, and somehow have it number each line where there is a duplicate (tried a groups query, but "count" won't work), then do an update query to change the duplicate to...
3
9131
by: RAG2007 | last post by:
I'm using the QueryDef and Execute method to update a record in my MySQL backend. Problem: When the Passthrough update query is defined using QueryDef, it becomes a select query, and I cannot use the Execute Method. Instance: The following code is defined as a query called "AddCost" UPDATE tblinitiative SET tblinitiative.Estimate = " & Estimate & ", tblinitiative.FinalCost = " & FinalCost & " WHERE (((tblinitiative.InitID)=" & InitID &...
1
3152
by: giovannino | last post by:
Dear all, I did a query which update a sequence number (column NR_SEQUENZA) in a table using a nice code (from Trevor !). 1) Given that I'm not a programmer I can't understand why numbering doesn't start always, running more times the update query, from the same starting parameter assigned (1000000000). It seems to me that it takes memory last number calculated and running prox update it starts from last table row updated. I need...
0
9767
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10711
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10807
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7104
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5774
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5967
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4592
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4190
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3215
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.