简体   繁体   English

如何从vb中的ms访问中检索字段

[英]how to retrieve a field from ms access in vb

I need to access the salary field of an employee table based on the entered username from ms access in vb6.. I read the username and password in a form and enter into another form that displays the salary corresponding to the username. 我需要根据输入的用户名来访问员工表的工资字段,该用户名来自vb6中的ms访问。我在表单中读取用户名和密码,然后输入另一个显示与用户名对应的工资的表单。 I am using Adodc1 connection. 我正在使用Adodc1连接。 I know SQL but dunno how to implement it in vb... I want to know exactly where to use the sql query? 我知道SQL但不知道如何在vb中实现它...我想知道在哪里使用sql查询? Thanks 谢谢

If you are using DAO, this should help. 如果您使用DAO,这应该会有所帮助。 Add the following functions to a module, and pass in MyDB a reference to the OPENED database (of datatype DAO.Database, use the DAO.OpenDatabase() function to open a database): 将以下函数添加到模块,并在MyDB中传递对OPENED数据库(数据类型为DAO.Database,使用DAO.OpenDatabase()函数打开数据库)的引用:

Public Function GetQueryResults(ByRef MyDB as DAO.Database, SQLQuery As String) As DAO.Recordset
    Dim Q As DAO.QueryDef, R As DAO.Recordset

    Set Q = MyDB.CreateQueryDef("", SQLQuery)
    Set R = Q.OpenRecordset

    Set GetQueryResults = R
End Function

Public Function GetFirstValueFromQuery(MyDB As DAO.Database, SQLQuery As String) As String
    If (MyDB Is Nothing) Then Exit Function

    Dim RES As DAO.Recordset, T As String

    Set RES = GetQueryResults(MyDB, SQLQuery)

    With RES
        T = .Fields(0).Value
        GetFirstValueFromQueryGeneral = T
    End With

    RES.Close
End Function

now, call this function from every form (wherever u need to run a SQL query): 现在,从每个表单调用此函数(无论您需要运行SQL查询):

Dim A as String
A=GetFirstValueFromQuery(MyDatabase, "SELECT Employee.Salary FROM Employee WHERE Employee.UserName='"+uname+"'")
Msgbox "Salary="+A

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM