首页 > 解决方案 > 如何以任何顺序从 MySQL 列直接获取数据到文本框?文本框 1 = 列 1,文本框 2 = 列 4,文本框 3 = 列 8

问题描述

现在我正在使用“SELECT * table...”和一个记录集通过记录集字段的编号将值放入文本框textbox1 = rs.Fields(0) textbox2 = rs.Fields(1)等等......它有效,但每次我对数据库进行更改时痛苦...把所有东西放回原处,无论如何使用数据库的列名将值放入文本框中?textbox1 = Column1 textbox2 = Column2因此向数据库添加新列并不重要,因为它们将由列标题选择。

我正在使用它来使用excel中的用户表单更新数据库中的值,为此我正在使用“UPDATE table SET COLUMN1 = textbox1 COLUMN2 = textbox2 ....”无论如何我可以像我一样得到它们更新所以如果我改变列数它不会变得混乱

我有的:

sqlQuery = "SELECT * FROM table WHERE OS ='123'
sqlQuery = sqlQuery & ";"
rs.Source = sqlQuery
Set rs.ActiveConnection = Conn
rs.CursorLocation = adUseClient
rs.Open


For Each field In rs.Fields
    If IsNull(rs.Fields(0)) = False Then Textbox1 = rs.Fields(0)
    If IsNull(rs.Fields(1)) = False Then Textbox2 = rs.Fields(1)
    If IsNull(rs.Fields(2)) = False Then Textbox3 = rs.Fields(2)
    If IsNull(rs.Fields(3)) = False Then Textbox4 = rs.Fields(3)
Next

我需要的:

sqlQuery = "SELECT * FROM table WHERE OS ='123'
sqlQuery = sqlQuery & ";"
rs.Source = sqlQuery
Set rs.ActiveConnection = Conn
rs.CursorLocation = adUseClient
rs.Open


For Each field In rs.Fields
    If IsNull(rs.Fields(0)) = False Then Textbox1 = Column1Header
    If IsNull(rs.Fields(1)) = False Then Textbox2 = Column2Header
    If IsNull(rs.Fields(2)) = False Then Textbox3 = Column3Header
    If IsNull(rs.Fields(3)) = False Then Textbox4 = Column4Header
next

标签: mysqlexcelvba

解决方案


rs.Fields("FieldName")作品

由于Fields是记录集对象的默认设置,因此rs("FieldName")也可以使用

您不必使用索引值。


推荐阅读