VBA 数据库同一表的当前行与其他行的主键重复判断实现方案

目的,判断是否主键重复,不重复则登录新数据,重复则不登录。

定义类型:

DataRecord

tableName 表名

rowNumber 行号

columnName 列名

data 数据

想要实现的代码逻辑如下:

模拟数据库的登录过程。假设现在登录了5条数据,要登录第6条数据。
规则1 ,判断其他5行数据的所有列,与第6行已经录的所有列的数据一一比较,如果存在不一致的,说明主键不重复,可以登录新的数据。
规则2 ,如果不存在一致的,则判断我们这次要登录的这个列的其他行的数据,是否存在不一致的,如果不一致则主键不重复,可以登录新的数据。
规则3,如果仍然没有不一致的数据,说明主键重复,不能登录这条数据。

代码结构如下:

私有DataRecordType类

模块1

DataRecordType类代码

vba 复制代码
' DataRecordType 类模块
Private pTableName As String
Private pRowNumber As Long
Private pColumnName As String
Private pData As String

' 定义公共属性以访问私有变量
Public Property Get tableName() As String
    tableName = pTableName
End Property
Public Property Let tableName(value As String)
    pTableName = value
End Property

Public Property Get rowNumber() As Long
    rowNumber = pRowNumber
End Property
Public Property Let rowNumber(value As Long)
    pRowNumber = value
End Property

Public Property Get columnName() As String
    columnName = pColumnName
End Property
Public Property Let columnName(value As String)
    pColumnName = value
End Property

Public Property Get data() As String
    data = pData
End Property
Public Property Let data(value As String)
    pData = value
End Property

模块1代码

vba 复制代码
Sub AddData(dataCollection As Collection, tableName As String, rowNumber As Long, columnName As String, data As String)
    Dim record As DataRecordType
    Set record = New DataRecordType  ' 关键:实例化对象
    record.tableName = tableName
    record.rowNumber = rowNumber
    record.columnName = columnName
    record.data = data
    dataCollection.Add record
End Sub

Function InsertData(dataCollection As Collection, tableName As String, rowNumber As Long, columnName As String, data As String) As Boolean
    Dim i As Long
    Dim sameTableRowData As String
    Dim otherTableRowData As String
    Dim record1 As DataRecordType
    Dim record2 As DataRecordType
    
    Dim noexist As Boolean
    
    For i = 1 To dataCollection.Count
        Set record1 = dataCollection(i)  ' 关键:从集合中获取对象
        If record1.tableName = tableName And record1.rowNumber = rowNumber Then
            For j = 1 To dataCollection.Count
                Set record2 = dataCollection(j)
                If record2.tableName = record1.tableName And record2.columnName = record1.columnName And record2.rowNumber <> record1.rowNumber Then
                    If record2.data <> record1.data Then
                        Call AddData(dataCollection, tableName, rowNumber, columnName, data)
                        InsertData = True
                        Exit Function
                    End If
                    Exit For
                End If
            Next j
        End If
    Next i
    
    For j = 1 To dataCollection.Count
        Set record2 = dataCollection(j)
        If record2.tableName = tableName And record2.columnName = columnName And record2.rowNumber <> rowNumber Then
            If record2.data <> data Then
                Call AddData(dataCollection, tableName, rowNumber, columnName, data)
                InsertData = True
                Exit Function
            End If
            Exit For
        End If
    Next j
    
    InsertData = False
End Function

Sub TestInsertData()
    Dim dataCollection As Collection
    Set dataCollection = New Collection
    
    ' 添加前 5 条数据
    Call AddData(dataCollection, "Table1", 1, "Column1", "Data1")
    Call AddData(dataCollection, "Table1", 1, "Column2", "Data2")
    Call AddData(dataCollection, "Table1", 1, "Column3", "Data3")
    Call AddData(dataCollection, "Table1", 2, "Column1", "Data1")
    Call AddData(dataCollection, "Table1", 2, "Column3", "Data3")
    
    ' 插入第 6 条数据并检查结果
    Dim result As Boolean
    result = InsertData(dataCollection, "Table1", 2, "Column2", "Data2")
    
    If result Then
        MsgBox "第 6 条数据已插入。"
    Else
        MsgBox "第 6 条数据未插入(与现有数据重复)。"
    End If
End Sub

运行TestInsertData()

相关推荐
睡前要喝豆奶粉1 分钟前
EF Core动态sql
数据库·sql·c#·.netcore
p***s913 分钟前
mysql用户名怎么看
数据库·mysql
5***g2295 分钟前
Ubuntu 系统下安装 Nginx
数据库·nginx·ubuntu
3***g2058 分钟前
SQL sever数据导入导出实验
数据库·sql·oracle
f***686011 分钟前
在Django中安装、配置、使用CKEditor5,并将CKEditor5录入的文章展现出来,实现一个简单博客网站的功能
数据库·django·sqlite
必胜刻14 分钟前
Go连接Mysql数据库
数据库·mysql·golang
l***466816 分钟前
使用mysql报Communications link failure异常解决
数据库·mysql
合作小小程序员小小店18 分钟前
桌面开发,食堂卡管理系统开发,基于C#,winform,mysql数据库
数据库·mysql·c#
o***111421 分钟前
【MySQL】MySQL库的操作
android·数据库·mysql
一 乐22 分钟前
游戏账号交易|基于Springboot+vue的游戏账号交易系统(源码+数据库+文档)
java·前端·数据库·vue.js·spring boot·后端·游戏