mdb1 数据库中有点表和线表,点表中有字段:管线点号,x坐标,y坐标,线表中有管线起点点号,管线终点点号。mdb2 数据库中有点表和线表,点表中有字段:点号,x,y。如何用 vba 语句将 mdb1 中的字段按类似,管线点号=点号,的对应关系导入到 mdb2 中
时间: 2024-09-11 10:13:25 浏览: 39
要在VBA中将MDB1数据库中的点表数据按照管线点号对应的规则导入到MDB2数据库的点表中,你可以使用宏语言来编写脚本。这里是一个基本的示例,假设已经建立了连接到两个数据库的对象,分别是`mdb1Conn`和`mdb2Conn`:
```vba
Sub ImportData()
Dim mdb1Rs As DAO.Recordset
Dim mdb2Rs As DAO.Recordset
Dim mdb1LineTable As DAO.TableDef
Dim mdb2PointTable As DAO.TableDef
Dim mdb1PipelineRow As DAO.Recordset
Dim mdb2PointRow As DAO.Recordset
Dim strSQL As String
' Set up the recordsets for point tables
Set mdb1Rs = mdb1Conn.OpenRecordset("mdb1.PointTable")
Set mdb2Rs = mdb2Conn.OpenRecordset("mdb2.PointTable", dbAppendOnly, dbFailOnError)
' Loop through each row in the mdb1.LineTable
For Each mdb1LineTable In mdb1Conn.Database.TableDefs
If mdb1LineTable.Name = "LineTable" Then
Set mdb1PipelineRow = mdb1LineTable.OpenRecordset(dbOpenDynaset)
' Check if there is a valid start and end point
If Not mdb1PipelineRow.EOF And Not mdb1PipelineRow.BOF Then
' Use the PipelineStartPointNo to find the corresponding start point in mdb1.PointTable
strSearch = "管线点号=" & mdb1PipelineRow!管线起点点号
Set mdb1PointRow = mdb1Rs.Open(strSearch, , dbOpenDynamic)
' Now copy the start point data to mdb2
If Not mdb1PointRow.EOF Then
For i = 0 To mdb1PointRow.Fields.Count - 1
mdb2Rs.Fields(i).Value = mdb1PointRow.Fields(i).Value ' Copy fields x, y
Next i
mdb2Rs.AddNew ' Add new record to mdb2.PointTable
mdb2Rs.Update ' Save the changes
End If
' Move to next start point
mdb1PointRow.MoveNext
Else
MsgBox "Invalid line record found."
End If
mdb1PipelineRow.MoveNext ' Move to next line
End If
Next
' Close all recordsets
mdb1Rs.Close
mdb2Rs.Close
End Sub
```
这个脚本会查找mdb1.LineTable中每条线的起点点号,然后在mdb1.PointTable中找到相应匹配的点,并将其坐标复制到mdb2.PointTable的新记录中。
阅读全文