Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

I have the following SELECT within

Status
Not open for further replies.

TheRiver

IS-IT--Management
Dec 18, 2003
121
GB
I have the following SELECT within a module.

What I want to do is update the field census date based on the results of the Select Case.

So the module will update the field Census_date to the str_censusdate. But how do I write this in the Update query?

Select Case intAnswer
Case "Apr": str_censusdate = "20030430"
Case "May": str_censusdate = "20030531"
Case "Jun": str_censusdate = "20030630"
Case Else: Exit Function
End Select


strSQL = &quot;UPDATE [WL CMDS (RNA00)] SET Census_Date = str_censusdate &quot; & &quot;WHERE Census_Date <> str_censusdate&quot;
Create.Execute strSQL
 
In Access SQL this can be done using IIF or Switch

IIF is better upto 3 conditions after that it becomes less legible
Syntax is
Switch(Condition1,Expression1,Condition2,Expression2...goes on)

strSQL = &quot;UPDATE [WL CMDS (RNA00)] SET Census_Date = switch(intAnswer =&quot;Apr&quot;; &quot;20030430&quot;;intAnswer =&quot;May&quot;; &quot;20030531&quot;
)&quot; & &quot;WHERE Census_Date <> switch(intAnswer =&quot;Apr&quot;; &quot;20030430&quot;;intAnswer =&quot;May&quot;; &quot;20030531&quot;
)&quot;

Best of luck
 
Is it not possible to somehow just pull across str_censusdate in the update bit?

All of this will be done within a module.
 
This is assumed your Census_Date is a text field:
Code:
strSQL = &quot;UPDATE [WL CMDS (RNA00)] SET Census_Date = '&quot; & str_censusdate & &quot;' WHERE Census_Date <> '&quot; & str_censusdate & &quot;'&quot;
DoCmd.RunSQL strSQL

If your Census_Date is a Date/Time field, you must do it differently:
Code:
strSQL = &quot;UPDATE [WL CMDS (RNA00)] SET Census_Date = #&quot; & left(str_censusdate,4) & &quot;/&quot; & Mid(str_censusdate,5,2) & &quot;/&quot; & right(str_censusdate,2) & &quot;# WHERE Census_Date <> str_censusdate&quot;
DoCmd.RunSQL strSQL

Does this help?
Regards,
MakeItSo

Andreas Galambos
EDP / Technical Support Specialist
(andreas.galambos@bowneglobal.de)
HP:
 
That worked great 'Makeitso' thanks for your help.
 
More help needed. As part of this module, before I do the update I import data from a txt file. What I want to be able to do is only update the newly imported files and not things that already exist within the table. Is this possible? If so how?
 
Do you import directly to the table or into a temp table first?
If not: you could do so and then use this temp table as query criterion:

strSQL = &quot;UPDATE [WL CMDS (RNA00)] SET Census_Date = #&quot; & left(str_censusdate,4) & &quot;/&quot; & Mid(str_censusdate,5,2) & &quot;/&quot; & right(str_censusdate,2) & &quot;# &quot; _
& &quot;FROM (your table) a inner join (temp table) b &quot; _
& &quot;ON a.[RecordID] = b.[RecordÍD] &quot; _
& &quot;WHERE Census_Date <> str_censusdate&quot;

Adapt the names &quot;your table&quot; and &quot;temp table&quot; as well as [RecordID] to your respective table/field names.

That will do the job. ;-)

Andreas Galambos
EDP / Technical Support Specialist
(andreas.galambos@bowneglobal.de)
HP:
 
I used this to get what I need. Another question is, is it posiible to somehow replace the 20030430 with a standard format to save me going into the module each year and updating it?

Select Case intAnswer
Case &quot;Apr&quot;: str_censusdate = &quot;20030430&quot;
Case &quot;May&quot;: str_censusdate = &quot;20030531&quot;
Case &quot;Jun&quot;: str_censusdate = &quot;20030630&quot;
Case &quot;Jul&quot;: str_censusdate = &quot;20030731&quot;
Case &quot;Aug&quot;: str_censusdate = &quot;20030831&quot;
Case &quot;Sep&quot;: str_censusdate = &quot;20030930&quot;
Case &quot;Oct&quot;: str_censusdate = &quot;20031031&quot;
Case &quot;Nov&quot;: str_censusdate = &quot;20031130&quot;
Case &quot;Dec&quot;: str_censusdate = &quot;20031231&quot;
Case &quot;Jan&quot;: str_censusdate = &quot;20040131&quot;
Case &quot;Feb&quot;: str_censusdate = &quot;20040228&quot;
Case &quot;Mar&quot;: str_censusdate = &quot;20040331&quot;
Case Else: Exit Function
End Select

Create.Execute &quot;UPDATE [WL CMDS (RNA00)] SET Census_Date = '&quot; & str_censusdate & &quot;' WHERE Census_Date <> '&quot; & str_censusdate & &quot;'&quot;
 
Try this:
Code:
Dim mon as string, lastd as string
mon=Month(&quot;2004-&quot; & intanswer & &quot;-30&quot;)
if len(mon)=1 then mon = &quot;0&quot; & mon
select case mon
case &quot;02&quot;
  lastd=&quot;28&quot;
case &quot;01&quot; or &quot;03&quot; or &quot;05&quot; or &quot;07&quot; or &quot;08&quot; or &quot;10&quot; or &quot;12&quot;
  lastd=&quot;31&quot;
case else
  lastd=&quot;30&quot;
str_censusdate=Year(Date()) & mon & lastd

Only trouble here: What if Feb has 29 days? You could catch this with If Year(Date()) \ 4 = 0 AND Year(Date()) \ 100 <> 0 then
lastd=&quot;29&quot;
Else
lastd=&quot;28&quot;

;-)
 
P.S: You can leave &quot;2004&quot; in this case, even if you switch to 2005... It's just to get the month value of Apr, Mar ...
 
where would I put this around the existing code?
 
Just replace your entire select case block with it.
So your code block from above will look like this:
Code:
Dim mon as string, lastd as string
...
mon=Month(&quot;2004-&quot; & intanswer & &quot;-30&quot;)
if len(mon)=1 then mon = &quot;0&quot; & mon
if CInt(mon)<1 or CInt(mon)>12 then goto skipthis'No valid month
select case mon
case &quot;02&quot;
  If Year(Date()) \ 4 = 0 AND Year(Date()) \ 100 <> 0 then
   lastd=&quot;29&quot; 
  Else
   lastd=&quot;28&quot;
  End If
case &quot;01&quot; or &quot;03&quot; or &quot;05&quot; or &quot;07&quot; or &quot;08&quot; or &quot;10&quot; or &quot;12&quot;
  lastd=&quot;31&quot;
case else
  lastd=&quot;30&quot;
str_censusdate=Year(Date()) & mon & lastd

Create.Execute &quot;UPDATE [WL CMDS (RNA00)] SET Census_Date = '&quot; & str_censusdate & &quot;' WHERE Census_Date <> '&quot; & str_censusdate & &quot;'&quot;

...

skipthis:
End Function


 
What do you mean: &quot;Doesn't like&quot;?
Have you inserted the line before End Function (with colon):
skipthis:
End Function
 
It works kind of now, but when I import data in which should have month on 20031130, it gives it a 20041130 figure
 
Ooops, missed something:
Have another:
Code:
Dim yr as String

and replace this line:
Code:
str_censusdate=Year(Date()) & mon & lastd
with these:
Code:
if mon > Month(Date()) then 
  yr = CStr(CInt(Year(Date())-1))
else
  yr = CSrt(Year(Date()))
End If
str_censusdate=yr & mon & lastd

[afro]
 
It still giving problems, remember that I will be running these modules each month based on financial years.

So Apr03 to Mar04.

At the moment if I add Apr 03 data I get 20040430 and when I run Nov03 data I get 20031130!!!!
 
based on financial years. So Apr03 to Mar04.
Didn't know that - have patience with non-business-men... ;-)
Anyway, there still was a little bug in there.
Replace
if mon > Month(Date()) then
yr = CStr(CInt(Year(Date())-1))
else
yr = CSrt(Year(Date()))
End If

with

[blue]
[green]'*****Version a: Up to for Jan: 2004, else 2003(monthly)[/green]
If CInt(mon) > Month(Date) Then
yr = CStr(CInt(Year(Date) - 1))
Else
yr = CStr(Year(Date))
End If

[green]'*****Version b: Up to Mar: 2004, else 2003 [/green](quarterly)
If CInt(mon) \ 4 > Month(Date) \ 4 Then
yr = CStr(CInt(Year(Date) - 1))
Else
yr = CStr(Year(Date))
End If

[/blue]

Hope this gives you correct results now.

P.S: Always back-up your db before doing mass-updates...
;-)
 
Still a bit more work!!!

when I load Jan 04 do I need to change anything?

When i load it at the moment I get result of 20040130 and when I load Mar 04 data I get 20030330
 
OK. Concerning the March 03 problem:
replace
[red]
If CInt(mon) \ 4 > Month(Date) \ 4 Then
yr = CStr(CInt(Year(Date) - 1))
Else
yr = CStr(Year(Date))
End If
[/red]
with
[blue]
If (CInt(mon)-1) \ 3 > (Month(Date)-1) \ 3 Then
yr = CStr(CInt(Year(Date) - 1))
Else
yr = CStr(Year(Date))
End If
[/blue]

Concerning 30th of Jan instead of 31st:
Although the above code should work, you might try replacing
[red]
mon=Month(&quot;2004-&quot; & intanswer & &quot;-30&quot;)
[/red]
with
[blue]
mon=CStr(Month(&quot;2004-&quot; & intanswer & &quot;-30&quot;))
[/blue]

This is straining... :cool:
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top