我在Access中有一个表,如下所示:
+----------+----------+-----------+
| AGP_ID | PRAD_ID | PRAD_TYPE |
+----------+----------+-----------+
| 00803110 | 00803110 | 002 |
| 00803110 | 00803110 | PRI |
| 00803153 | 00803153 | 002 |
| 00803153 | 00803153 | 003 |
| 00803153 | 00803153 | PRI |
+----------+----------+-----------+
我想为每个AGP ID添加一个列调用Days和该列,然后我希望每个AGP ID都有M,T,W,TH,F,Sat,Sun。新表格如下:
+----------+----------+-----------+-----+
| 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 |
+----------+----------+-----------+-----+
任何帮助将不胜感激。我一直把头发拉过来,但与我做过的其他事情相比,它似乎很简单。
感谢。
答案 0 :(得分:0)
不确定是否循环浏览每条记录是个好主意。你问过它..试一试;)
将此代码放在按钮下单击oder a sub。
在设计模式下打开表格并插入新列Day作为文本(2个字符) 修改select / update sql命令。
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:你可以删除你真的不需要的beginTrans / commitTrans。我把它用于示范。我还没有测试过这段代码,但应该可以工作..