I have a table in Access like the following:
+----------+----------+-----------+
| AGP_ID | PRAD_ID | PRAD_TYPE |
+----------+----------+-----------+
| 00803110 | 00803110 | 002 |
| 00803110 | 00803110 | PRI |
| 00803153 | 00803153 | 002 |
| 00803153 | 00803153 | 003 |
| 00803153 | 00803153 | PRI |
+----------+----------+-----------+
I want to add a column call Days and in that column for each AGP ID, Then I want to have M, T, W, TH, F, Sat, Sun for each AGP ID. The new table will look like:
+----------+----------+-----------+-----+
| AGP_ID | PRAD_ID | PRAD_TYPE | Day |
+----------+----------+-----------+-----+
| 00803110 | 00803110 | 002 | M |
| 00803110 | 00803110 | PRI | T |
| 00803110 | | | W |
| 00803110 | | | TH |
| 00803110 | | | F |
| 00803110 | | | Sat |
| 00803110 | | | Sun |
| 00803153 | 00803153 | 002 | M |
| 00803153 | 00803153 | 003 | T |
| 00803153 | 00803153 | PRI | W |
| 00803153 | | | TH |
| 00803153 | | | F |
| 00803153 | | | Sat |
| 00803153 | | | Sun |
+----------+----------+-----------+-----+
Any help will be appreciated. I've been pulling my hair over this but it seems so simple compare to the other things I have done.
Thanks.
Not sure if its good idea to loop through each records. you asked for it.. give it a try ;)
throw this code under button click oder a sub.
Open the table in design mode and insert new column Day as text (2 char) modify the select/update sql command.
Dim myDB As DAO.Database
Dim myRs As DAO.Recordset
Set myDB = CurrentDb
Dim SQL_GET As String
SQL_GET = "SELECT ID from YOUR_TAbLE"
Set myRs = myDB.OpenRecordset(SQL_GET)
Dim mRc As Long
mRc = Nz(myRs.RecordCount, 0)
If Not mRc = 0 Then
Dim dArr(6) As String
dArr(0) = "MO"
dArr(1) = "TU"
dArr(2) = "WE"
dArr(3) = "TH"
dArr(4) = "FR"
dArr(5) = "SA"
dArr(6) = "SU"
Dim SQL_SET As String
Dim I As Integer
I = 0
While Not myRs.EOF
SQL_SET = "UPDATE Your_table SET Day='" & dArr(I) & "' WHERE (ID = " & Nz(myRs("id"), "") & ")"
On Error GoTo ERROR_SQL1:
DBEngine.BeginTrans
myDB.Execute SQL_SET, dbFailOnError
DBEngine.CommitTrans
myRs.MoveNext
If I < 6 Then I = I + 1 Else I = 0
Wend
Else
msgbox "No records found.. :("
End If
EXIT_SUB:
On Error Resume Next
Set myDB = Nothing
Exit Sub
ERROR_SQL1:
DBEngine.Rollback
MsgBox "Error while executing sql_1. " & vbNewLine & "System msg: " & Err.description
GoTo EXIT_SUB
PS: you can remove the beginTrans/commitTrans you don't really need that. i put it for demonstration. also i haven't tested this code but should work..
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.