ms access 数据库自动启动再次存储第一行的数据

Posted

技术标签:

【中文标题】ms access 数据库自动启动再次存储第一行的数据【英文标题】:Ms access database is automatically started to store data from 1st row again 【发布时间】:2019-10-03 21:07:04 【问题描述】:

我正在使用 vb6 将数据存储在数据库中。它希望工作正常,直到 150 条记录(数据库中的行),但之后数据从第一行存储,即使我按升序对其进行排序,它也不会在数据网格中排序,因此我在获取时遇到问题我的下一个菜单形式的订单号(关于我的 vb6 项目)。

Private Sub CmdDone_Click()
Dim a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, Y, z, aa, bb, cc, dd, ee, ff, gg, hh, ii, jj, kk As Integer
Dim ll, mm, nn, oo, pp, qq, rr, ss As Integer
Dim flag As Integer
flag = 0
Set db = OpenDatabase("D:\OrderMania\ordermania.mdb")
Set rs = db.OpenRecordset("select * from order1")
If rs.RecordCount > 0 Then
rs.MoveLast
GoTo 1
End If
1 If StrConv(TxtName.Text, vbProperCase) = "" Then
    MsgBox ("Please Enter Your Name"), vbExclamation
    TxtName.SetFocus
    flag = 1
End If
If ComboTable.Text = "Select" Then
       MsgBox ("Please Select The Table Number"), vbExclamation
       ComboTable.SetFocus
       flag = 1
 Else
        If Check1.Value = 1 Then
        rs.AddNew
        rs.Fields(7).Value = StrConv(TxtName.Text, vbProperCase)
        rs.Fields(0).Value = TextOrNO.Text
        rs.Fields(1).Value = ComboTable.Text
        rs.Fields(6).Value = TxtDate.Text
        rs.Fields(3).Value = UserControl1.Text
        rs.Fields(2).Value = Check1.Caption
        rs.Fields(4).Value = "20"
        rs.Fields(5).Value = UserControl1.Text * 20
        a = UserControl1.Text * 20
        rs.Fields(8).Value = "Pending"
        rs.Fields(9).Value = "Waiting"
        rs.Update
        End If

        If Check2.Value = 1 Then
        rs.AddNew
        rs.Fields(7).Value = StrConv(TxtName.Text, vbProperCase)
        rs.Fields(0).Value = TextOrNO.Text
        rs.Fields(1).Value = ComboTable.Text
        rs.Fields(6).Value = TxtDate.Text
        rs.Fields(3).Value = UserControl2.Text
        rs.Fields(2).Value = Check2.Caption
        rs.Fields(4).Value = "15"
        rs.Fields(5).Value = UserControl2.Text * 15
        b = UserControl2.Text * 15
        rs.Fields(8).Value = "Pending"
        rs.Fields(9).Value = "Waiting"
        rs.Update

        End If

        If Check3.Value = 1 Then
        rs.AddNew
        rs.Fields(7).Value = StrConv(TxtName.Text, vbProperCase)
        rs.Fields(0).Value = TextOrNO.Text
        rs.Fields(1).Value = ComboTable.Text
        rs.Fields(3).Value = UserControl3.Text
        rs.Fields(6).Value = TxtDate.Text
        rs.Fields(2).Value = Check3.Caption
        rs.Fields(4).Value = "12"
        rs.Fields(5).Value = UserControl3.Text * 12
        c = UserControl3.Text * 12
        rs.Fields(8).Value = "Pending"
        rs.Fields(9).Value = "Waiting"
        rs.Update

        End If

        If Check4.Value = 1 Then
        rs.AddNew
        rs.Fields(7).Value = StrConv(TxtName.Text, vbProperCase)
        rs.Fields(0).Value = TextOrNO.Text
        rs.Fields(1).Value = ComboTable.Text
        rs.Fields(3).Value = UserControl4.Text
        rs.Fields(6).Value = TxtDate.Text
        rs.Fields(2).Value = Check4.Caption
        rs.Fields(4).Value = "20"
        rs.Fields(5).Value = UserControl4.Text * 20
        d = UserControl4.Text * 20
        rs.Fields(8).Value = "Pending"
        rs.Fields(9).Value = "Waiting"
        rs.Update

        End If

        If Check5.Value = 1 Then
        rs.AddNew
        rs.Fields(7).Value = StrConv(TxtName.Text, vbProperCase)
        rs.Fields(0).Value = TextOrNO.Text
        rs.Fields(1).Value = ComboTable.Text
        rs.Fields(3).Value = UserControl5.Text
        rs.Fields(6).Value = TxtDate.Text
        rs.Fields(2).Value = Check5.Caption
        rs.Fields(4).Value = "20"
        rs.Fields(5).Value = UserControl5.Text * 20
        e = UserControl5.Text * 20
        rs.Fields(8).Value = "Pending"
        rs.Fields(9).Value = "Waiting"
        rs.Update

        End If
End If
TxtTotal.Text = a + b + c + d + e + f + g + h + i + j + k + l + m + n + o + p + q + r + s + t + u + v + w + x + Y + z + aa + bb + cc + dd + ee + ff + gg + hh + ii + jj + kk + ll + mm + nn + oo + pp + qq + rr + ss
Tot = CInt(TxtTotal.Text)
If flag = 0 Then
MsgBox "Your Order has been Placed", vbOKOnly, "Done"
End If
End Sub

Private Sub cmdBill_Click()
Dim Tot As Integer
Dim gst, gtot As Double
Tot = 0
Set db = OpenDatabase("D:\OrderMania\ordermania.mdb")
Set rs = db.OpenRecordset("select * from order1")


While Not rs.EOF
  If StrConv(TxtName.Text, vbProperCase) = rs.Fields(7).Value And TextOrNO.Text = rs.Fields(0).Value Then

      Form7.List1.AddItem (rs.Fields(2).Value)
      Form7.List2.AddItem (rs.Fields(4).Value)
      Form7.List3.AddItem (rs.Fields(3).Value)
      Form7.List4.AddItem (rs.Fields(5).Value)
      Form7.Text2.Text = rs.Fields(1).Value
      Form7.Text7.Text = rs.Fields(6).Value
      Tot = Tot + rs.Fields(5).Value
    End If
rs.MoveNext
Wend
Form7.Text6.Text = Tot
gst = Tot * 0.05
Form7.Text5.Text = gst
gtot = Tot + gst
Form7.Text4.Text = gtot
Form7.Text3.Text = StrConv(TxtName.Text, vbProperCase)
Form7.Text1.Text = TextOrNO.Text
Form7.Show
Unload Me

End Sub


Private Sub Form_Activate()
TxtName.SetFocus
End Sub
Private Sub generateOID()
Dim db1 As Database
Dim rs1 As Recordset
Set db1 = OpenDatabase("D:\OrderMania\ordermania.mdb")
Set rs1 = db1.OpenRecordset("select * from order1")
If rs1.EOF = True Then
TextOrNO.Text = 1
Exit Sub
Else
rs1.MoveLast
TextOrNO.Text = rs1.Fields(0).Value + 1
End If

End Sub
Private Sub Form_Load()

Call generateOID
TxtName.Text = ""
ComboTable.Text = "Select"
TxtDate.Text = Date
lblTime.Caption = Time
TextOrNO.Enabled = False
End Sub

【问题讨论】:

【参考方案1】:

看起来您正在使用TextOrNO.Text 作为您的订单号,但在下订单后您并没有增加该值。

generateOID中,修改SELECT语句对数据进行排序:

Private Sub generateOID()
    Dim db1 As Database
    Dim rs1 As Recordset

    Set db1 = OpenDatabase("D:\OrderMania\ordermania.mdb")
    Set rs1 = db1.OpenRecordset("SELECT * FROM order1 ORDER BY odrno ASC")

    If rs1.EOF = True Then
        TextOrNO.Text = 1
        Exit Sub
    Else
        rs1.MoveLast
        TextOrNO.Text = rs1.Fields(0).Value + 1
    End If

End Sub

【讨论】:

以上是关于ms access 数据库自动启动再次存储第一行的数据的主要内容,如果未能解决你的问题,请参考以下文章

使用 OLE 时如何绕过 MS Access 启动?

MS Access 主键自动编号变得混乱

VBA Excel - 从 MS Access 将列名保存到电子表格

MS Access 库存和自动增量值

最后插入行的自动编号值 - MS Access / VBA

需要连接到 MS Access 数据库并每 30 分钟自动将它们填充到 mysql 表中