我写了一个抽奖程序,抽中的号码就删掉了,下次再从数据库浏览剩余的号码,但运行是抽过的号码还是会出现,过程如下:
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

解决方案 »

  1.   

    strRs = "select * from mingdan where id=" & Int(Label2.Caption) & "" 
    改成这个试试
    strRs = "delete * from mingdan where id=" & Int(Label2.Caption) & "" 你的思路可以再改进一下
      

  2.   

    dao.......咸丰年代的东西了.....
      

  3.   

    Label2.Caption = Int(Rnd * rs!id)id乘Rnd后又取整还等于id吗?
      

  4.   

    给你看段我的程序 
    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
      

  5.   

    If Not rs.EOF Then 
            rs.Delete 
    这个就已经删除了啊
      

  6.   

    Label2.Caption 与 rs!id 相差十万八千里 
      

  7.   

        If Not rs.EOF Then 
            rs.Delete 
            rs.update
            rs.Close 
            db.Close 
            MsgBox "中奖者是:" & Label2.Caption & "号" 
        Else 
    看我的吧 加分 在加100哦 哈哈
      

  8.   

    rs.delete
    rs.update  '是不是要加上这个?
      

  9.   

    Label2.Caption 与 rs!id 不等
      

  10.   


    你是对的,我现在还在想怎么解决,rnd*rs!id 变成在rs!id 范围内随便取个数字了而不是取数据库中的数字
      

  11.   

    可以把记录放到数组里,根据rnd随机选取
      

  12.   

    可以把记录放到数组里,根据rnd随机选取
      

  13.   

    记录放数组里,用rnd随机选取
      

  14.   

    我是程序里 名字是读取文本文件里的 1行代表一个人的信息
    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
      

  15.   


    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结果下标越界
      

  16.   

    dim st
    dim txttemp
    txttemp=""
    Do not rs.EOF 
        txttemp=txttemp & trim(rs("id") & " ") & "|"  
        rs.MoveNext 
    Loop 
    ST = Split(txtTemp, "|")ST就是你要的数组了 
    再配合我7楼的代码
      

  17.   

    数组已经取得,但从数组中随机取数就实现不了了
    Label2.Caption = Int(Rnd * sid(i)) 
      

  18.   

    看见我7楼里这个代码吗?
    Randomize 
    i = CLng((UBound(ST) * Rnd)) 这个就是随机数字的产生 
    ST(I)就是抽奖结果  得了 这个程序读快帮你写好了
      

  19.   

    比如说,我现在数据库里只有两条记录14 和 30
    i = CLng((UBound(ST) * Rnd)) 
    这个等于是14范围内随机取一个数或者30内随机取一个数了
    删除过的数字还是会跳出来
      

  20.   

    http://clubfiles3.liba.com/2008/08/19/16/16046250.rar 你看看我的程序吧  有源代码的
      

  21.   


    Label2.Caption = sid(int(rnd*rs.RecordCount ))