我需要根据输入的用户名来访问员工表的工资字段,这是从vb6中的ms访问。我在表单中读取用户名和密码,然后输入另一个表单,显示与用户名对应的工资。我正在使用Adodc1连接。我知道SQL但不知道如何在vb中实现它...我想知道在哪里使用sql查询? 谢谢
答案 0 :(得分:2)
如果您使用DAO,这应该会有所帮助。将以下函数添加到模块,并在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
现在,从每个表单(无论你需要运行SQL查询的地方)调用此函数:
Dim A as String
A=GetFirstValueFromQuery(MyDatabase, "SELECT Employee.Salary FROM Employee WHERE Employee.UserName='"+uname+"'")
Msgbox "Salary="+A