Public a1 As Integer Public idnum As Long Dim rs1 As DAO.Recordset '定义记录集变量 Private Sub Command抽奖1_Click() If a1 = 1 Then a1 = 0 rs1.Close Else a1 = 1 Set rs1 = CurrentDb.OpenRecordset("奖励表", dbOpenTable) End If End Sub Private Sub Command抽奖2_Click() If a1 = 1 Then a1 = 0 rs1.Close DoCmd.SetWarnings (False) Dim del_sql As String del_sql = "Delete From 奖励表 Where ID=" & idnum DoCmd.RunSQL del_sql Else a1 = 1 Set rs1 = CurrentDb.OpenRecordset("奖励表", dbOpenTable) End If End Sub Private Sub Command重置_Click() DoCmd.SetWarnings (False) Dim del_sql As String del_sql = "Delete From 奖励表" DoCmd.RunSQL del_sql Dim update_sql As String update_sql = "insert into 奖励表 select * From 奖励备份表" DoCmd.RunSQL update_sql End Sub Sub 抽奖1() On Error GoTo 抽奖完成 If a1 = 1 Then Dim Record_count As Long Record_count = rs1.RecordCount Dim rnd_i As Long '-------------------------------------------------------- Randomize rnd_i = Int((Record_count - 1 + 1) * Rnd + 1) rs1.MoveFirst rs1.Move (rnd_i - 1) Me.奖励 = rs1.Fields("奖励名称").Value idnum = rs1.Fields("ID").Value End If Exit Sub 抽奖完成: MsgBox "抽奖完成,重新抽奖可重置" a1 = 0 rs1.Close End Sub Private Sub Form_Timer() If a1 = 1 Then Call 抽奖1 End If End Sub哪里出错了显示insetinto语句包含未知字段 “奖励”
时间: 2024-02-29 12:54:59 浏览: 124
这个错误是因为在代码中的以下行中,"奖励"字段没有被正确地指定为插入的表中的一个字段:
`update_sql = "insert into 奖励表 select * From 奖励备份表"`
请检查"奖励表"和"奖励备份表"中的字段是否完全匹配,如果不匹配,请将代码中的"奖励"字段更改为正确的字段名称。
相关问题
Option Compare Database Option Explicit Public a1 As Integer Public idnum As Long Dim rs1 As DAO.Recordset '定义记录集变量 Private Sub Command抽奖1_Click() If a1 = 1 Then a1 = 0 rs1.Close Else a1 = 1 Set rs1 = CurrentDb.OpenRecordset("奖励表", dbOpenTable) End If End Sub Private Sub Command抽奖2_Click() If a1 = 1 Then a1 = 0 rs1.Close DoCmd.SetWarnings (False) Dim del_sql As String del_sql = "Delete From 奖励表 Where ID=" & idnum DoCmd.RunSQL del_sql Else a1 = 1 Set rs1 = CurrentDb.OpenRecordset("奖励表", dbOpenTable) End If End Sub Private Sub Command重置_Click() DoCmd.SetWarnings (False) Dim del_sql As String del_sql = "Delete From 奖励表" DoCmd.RunSQL del_sql Dim update_sql As String update_sql = "insert into 奖励表 select * From 奖励备份表" DoCmd.RunSQL update_sql End Sub Sub 抽奖1() On Error GoTo 抽奖完成 If a1 = 1 Then Dim Record_count As Long Record_count = rs1.RecordCount Dim rnd_i As Long Randomize rnd_i = Int((Record_count - 1 + 1) * Rnd + 1) rs1.MoveFirst rs1.Move (rnd_i - 1) Me.奖励 = rs1.Fields("奖励名称").Value idnum = rs1.Fields("ID").Value End If Exit Sub 抽奖完成: MsgBox "抽奖完成,重新抽奖可重置" a1 = 0 rs1.Close End Sub Private Sub Form_Timer() If a1 = 1 Then Call 抽奖1 End If End Sub打开窗体之后点击抽奖按钮,但是奖励框中不显示奖励名称是为什么
根据提供的代码,可能是因为在抽奖1()子过程中,没有将抽中的奖励名称赋值给奖励框的属性。可以在抽奖1()子过程中添加一行代码,将抽中的奖励名称赋值给奖励框的属性,例如:
```
Me.奖励 = rs1.Fields("奖励名称").Value
```
其中,"奖励"是奖励框的属性名称,需要根据实际情况进行修改。
另外,建议在使用记录集变量时,先判断记录集是否已经打开,以避免出现异常情况。例如,在Command抽奖2_Click()子过程中,可以添加以下代码:
```
If rs1.State = adStateOpen Then
rs1.Close
End If
```
这样可以确保每次操作前记录集都已经关闭。
阅读全文