简体   繁体   中英

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. I am using Adodc1 connection. I know SQL but dunno how to implement it in vb... I want to know exactly where to use the sql query? Thanks

If you are using DAO, this should help. 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):

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):

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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