将带有总和的查询值分配给文本框

Assign Value from Query with sum to Textbox

我正在尝试将 SQL 查询中的值分配给文本框。

我在更新后将功能绑定到 ComboBox。

我用 运行 测试了 SQL。

如何将结果分配给名为 prepoffEIC 的 Txtbox?

Dim MyVar2 As Integer
MyVar2 = Me.SelectedExam.Column(0)
ExamViewQry = "SELECT Sum(tblentrys.entryhours) AS TotalHoursPerFunction FROM tBleExams INNER JOIN (tBlBankList INNER JOIN (tBlExaminers INNER JOIN (tBlEntrys INNER JOIN tBlActivity ON tBlEntrys.EntryActivityID = tBlActivity.FunctionKey) ON tBlExaminers.ExaminersKey = tBlEntrys.EntryExaminerID) ON tBlBankList.BankID = tBlEntrys.EntryInstitutionID) ON (tBlBankList.BankID = tBleExams.ExamBankID) AND (tBleExams.ExamID = tBlEntrys.EntryExamID) WHERE tBlEntrys.EntryActivityID=1 AND tblEntrys.EntryExamStageID=1 AND tBleExams.ExamID=" & MyVar2

Me.prepoffEIC.ControlSource = "ExamViewQry"
Me.prepoffEIC.Requery

使用您拥有的 sql 创建一个查询,但稍作修改后将其粘贴到此处:

PARAMETERS eid long;
SELECT Sum(tblentrys.entryhours) AS TotalHoursPerFunction
FROM tBleExams
INNER JOIN (
    tBlBankList INNER JOIN (
        tBlExaminers INNER JOIN (
            tBlEntrys INNER JOIN tBlActivity ON tBlEntrys.EntryActivityID = tBlActivity.FunctionKey
            ) ON tBlExaminers.ExaminersKey = tBlEntrys.EntryExaminerID
        ) ON tBlBankList.BankID = tBlEntrys.EntryInstitutionID
    ) ON (tBlBankList.BankID = tBleExams.ExamBankID)
    AND (tBleExams.ExamID = tBlEntrys.EntryExamID)
WHERE tBlEntrys.EntryActivityID = 1
    AND tblEntrys.EntryExamStageID = 1
    AND tBleExams.ExamID = [eid]

我们称它为 qryGetHours(因为我不知道你需要它做什么。) 在更新后事件中(也使用更好的命名,这又快又脏)

dim db as DAO.Database
dim qry as QueryDef
dim rs as DAO.Recordset

set db = currentdb
set qry = db.querydefs("qryGetHours")
'this is the name of the query you made above
qry.parameters("eid").value = me.SelectedExam.Column(0)
set rs = qry.openrecordset(dbopendynaset,dbseechanges)
'dbseechanges is more for if you have a sql server backend, but i usually do
if not ( rs.eof and rs.bof) then
    rs.movefirst
    me.prepoffEIC = rs.fields("TotalHoursPerFunction").value
    'This portion assumes that you only get one record back, 
    'or if you do end up with more than one, it only goes 
    'after the first one.
else
    msgbox "Errors... Errors everywhere."
    'You will definitely want to put something meaningful 
    'here relating to it not being able to find the data you 
    'were looking for.
end if
if not rs is nothing then
    rs.close
    set rs = nothing
end if
set qry = nothing
set db = nothing
'you will always want to do this portion where you properly 
'check if a recordset exists and then close it when you are 
'done, along with closing out the querydef and database variables.