我写了一个抽奖程序,抽中的号码就删掉了,下次再从数据库浏览剩余的号码,但运行是抽过的号码还是会出现,过程如下:
Private Sub Timer1_Timer()
Dim sid As Integer, strRs As String
Dim rs As DAO.Recordset
Set db = OpenDatabase(App.Path & "\sample.mdb")
strRs = "select * from mingdan"
Set rs = db.OpenRecordset(strRs)
Do Until rs.EOF
Label2.Caption = Int(Rnd * rs!id)
rs.MoveNext
Loop
If rs.RecordCount = 0 Then
MsgBox ("找不到合适的记录")
Timer1.Enabled = False
End If
rs.Close
db.Close
End SubPrivate Sub Command4_Click()
Timer1.Enabled = False
Set db = OpenDatabase(App.Path & "\sample.mdb")
Dim rs As DAO.Recordset
strRs = "select * from mingdan where id=" & Int(Label2.Caption) & ""
Set rs = db.OpenRecordset(strRs)
If Not rs.EOF Then
rs.Delete
rs.Close
db.Close
MsgBox "中奖者是:" & Label2.Caption & "号"
Else
MsgBox ("该号码已抽过")
End If
Command3.Enabled = True
End SubPrivate Sub Command3_Click()
Timer1.Enabled = True
Timer1_Timer
Command3.Enabled = False
End Sub
Private Sub Timer1_Timer()
Dim sid As Integer, strRs As String
Dim rs As DAO.Recordset
Set db = OpenDatabase(App.Path & "\sample.mdb")
strRs = "select * from mingdan"
Set rs = db.OpenRecordset(strRs)
Do Until rs.EOF
Label2.Caption = Int(Rnd * rs!id)
rs.MoveNext
Loop
If rs.RecordCount = 0 Then
MsgBox ("找不到合适的记录")
Timer1.Enabled = False
End If
rs.Close
db.Close
End SubPrivate Sub Command4_Click()
Timer1.Enabled = False
Set db = OpenDatabase(App.Path & "\sample.mdb")
Dim rs As DAO.Recordset
strRs = "select * from mingdan where id=" & Int(Label2.Caption) & ""
Set rs = db.OpenRecordset(strRs)
If Not rs.EOF Then
rs.Delete
rs.Close
db.Close
MsgBox "中奖者是:" & Label2.Caption & "号"
Else
MsgBox ("该号码已抽过")
End If
Command3.Enabled = True
End SubPrivate Sub Command3_Click()
Timer1.Enabled = True
Timer1_Timer
Command3.Enabled = False
End Sub
改成这个试试
strRs = "delete * from mingdan where id=" & Int(Label2.Caption) & "" 你的思路可以再改进一下
ST是个全局变量数组 把所有人的名字一枪头读入数组
随机产生的名字显示在LabXM 上
按停止后就把LabXM 里的数字ADDITEM 到LIST1中 红色部分就是和LIST1进行比较
Private Sub Timer1_Timer()
Dim i As Long
Dim t As IntegerLabXM = ""
LabDetail = ""
i = 0
R1:
Randomize
i = CLng((UBound(ST) * Rnd))
If i < 0 Then GoTo R1
If Check1.Value = 1 Then
For t = 1 To List1.ListCount
List1.ListIndex = t - 1
If Trim(Mid(List1, InStr(List1, "|") + 1)) = Trim(ST(i) & " ") Then
i = -1
Exit For
End If
Next
End If
If i < 0 Then GoTo R1
If LX = "" Then
LabXM = ST(i)
LabDetail = ST(i)
End If
End Sub
rs.Delete
这个就已经删除了啊
rs.Delete
rs.update
rs.Close
db.Close
MsgBox "中奖者是:" & Label2.Caption & "号"
Else
看我的吧 加分 在加100哦 哈哈
rs.update '是不是要加上这个?
你是对的,我现在还在想怎么解决,rnd*rs!id 变成在rs!id 范围内随便取个数字了而不是取数据库中的数字
Public ST
Public iCount As Long
Public LX As String
Private Sub Command1_Click()
'定义一个变量 先吧所有数据都到里面
Dim txtTemp
Dim Textline As String
CommonDialog1.CancelError = True
On Error GoTo ErrHandler
' 设置标志
CommonDialog1.Flags = cdlOFNHideReadOnly
' 设置过滤器
CommonDialog1.Filter = "TEXT Files (*.txt)|*.txt"
' 指定缺省的过滤器
CommonDialog1.FilterIndex = 2
' 显示“打开”对话框
CommonDialog1.ShowOpen
' 显示选定文件的名字
' CommonDialog1.FileName
txtTemp = ""
Open CommonDialog1.FileName For Input As #1 ' 打开配置文件。
Do While Not EOF(1)
Line Input #1, Textline
If Trim(Textline & " ") <> "" Then
txtTemp = txtTemp & "|" & Textline
End If Loop
Close #1
txtTemp = Mid(txtTemp, 2)
ST = Split(txtTemp, "|")
iCount = UBound(ST)
LX = ""
ErrHandler:
End Sub
Dim sid() As IntegerReDim sid(rs.RecordCount)
For i = 0 To rs.RecordCount
sid(i) = rs("id")
Next iDo Until rs.EOF
Label2.Caption = Int(Rnd * sid(i))
rs.MoveNext
Loop结果下标越界
dim txttemp
txttemp=""
Do not rs.EOF
txttemp=txttemp & trim(rs("id") & " ") & "|"
rs.MoveNext
Loop
ST = Split(txtTemp, "|")ST就是你要的数组了
再配合我7楼的代码
Label2.Caption = Int(Rnd * sid(i))
Randomize
i = CLng((UBound(ST) * Rnd)) 这个就是随机数字的产生
ST(I)就是抽奖结果 得了 这个程序读快帮你写好了
i = CLng((UBound(ST) * Rnd))
这个等于是14范围内随机取一个数或者30内随机取一个数了
删除过的数字还是会跳出来
Label2.Caption = sid(int(rnd*rs.RecordCount ))