如何在MS ACCESS中插入像Excel函数这样的新行

时间:2010-08-09 20:23:30

标签: excel ms-access excel-vba vba

我需要找到一种在 MS Access &仅通过代码VBA向其插入新行 确切地说,excel的表现如何。

2 个答案:

答案 0 :(得分:2)

你看过连续表格和数据表了吗?

在数据库中复制电子表格行为非常不是一个好主意

答案 1 :(得分:0)

Code InsertRows:在两行之间插入行

首先创建A暂时然后像主表一样处理这个表

代码就是和我一起工作 这里是代码:

 Sub InsertRows()
    On Error GoTo ErrorNu
    Dim SQLP As String
    Dim Con As New ADODB.Connection
    Dim Conx As New ADODB.Connection
    Dim Rst As New ADODB.Recordset
    Dim Rs As New ADODB.Recordset
    Dim Rsx As New ADODB.Recordset
    Dim Rn As New ADODB.Recordset
    Dim Rd As New ADODB.Recordset
    Dim Num As Long
    Dim intRows
    Dim arrEmployees As Variant
    Dim x As Integer, Y As Integer

    Set Con = CurrentProject.Connection
Con.BeginTrans

sqlo = " select max(AutoRec)as maxa from Note_Custom "
Set Rn = Con.Execute(sqlo)


SQLP = " SELECT AutoRec, TextCOspoId, OuerM,Note"
SQLP = SQLP & " , TextBillId,NuCOspoId,dateTybe FROM Note_Custom ORDER BY AutoRec"

Rs.Open SQLP, Con, adUseClient, adOpenStatic, adCmdText

sqlo = " UPDATE Note_Custom SET TextBillId = ''"
sqlo = sqlo & " WHERE  AutoRec > " & 0
Con.Execute (sqlo)


intRows = Val(Rn!maxa)
Num = 1
arrEmployees = Rs.GetRows(intRows)
Y = 0
    For x = 0 To intRows - 1
If x = Val(SelTop - 1) Then
Y = 1
Rs.AddNew
Rs![AutoRec] = arrEmployees(0, x)
Rs![TextBillId] = 1
Rs.Update
End If
Rs.AddNew
Rs![AutoRec] = arrEmployees(0, x) + Y
Rs![TextCOspoId] = arrEmployees(1, x)
Rs![OuerM] = arrEmployees(2, x)
Rs![Note] = arrEmployees(3, x)
Rs![NuCOspoId] = arrEmployees(5, x)
Rs![dateTybe] = arrEmployees(6, x)


Rs![TextBillId] = 1
Rs.Update
    Next x
    sqlo = "DELETE * FROM Note_Custom where TextBillId = """""
Con.Execute (sqlo)
Con.CommitTrans
SelFiled = Me.SelTop
Me.Requery
sqlo = "SELECT Last(AutoRec) AS LastAuto,First(AutoRec) AS FirstAuto,Count(AutoRec) AS CountAuto FROM Note_Custom"
    Set Rd = Con.Execute(sqlo)
If Me.SelTop <> AutoRec Or Rd!LastAuto <> Rd!CountAuto Then
Refix
End If
DoCmd.GoToRecord , , acGoTo, SelFiled
'Me.SelTop = SelFiled
If RecType = False Then
Forms![Ncustom]!Edite.Enabled = True
Forms![Ncustom]!Viewer1.Enabled = False
Forms![Ncustom]!DELETE.Enabled = False
End If
arrEmployees = Empty
Rs.Close
Con.Close
Set Rs = Nothing
Set Con = Nothing
Exit Sub
ErrorNu:
SelFiled = Me.SelTop
Me.Requery
Me.SelTop = SelFiled
End Sub