I am trying to assign the value from a SQL Query to a text box.
I have the function tied to a ComboBox After update.
I tested the SQL by running it.
How do I assign the result to the Txtbox named prepoffEIC?
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
Create a query using the sql you have, but slightly modded paste it here:
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]
lets call it qryGetHours (since i dont know what you need it for.)
in the after update event (also use better naming, this is quick and dirty)
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.
Related
I need to transfer some data from one table to another.
The problem is that my current code switches automatically to the first three rows of another table, and not to the necessary rows.
It's about the fact that in one table (Hidd1) I have depths of 0m, 5m, 10m, 20m... and the bottom, and in the other (FdSest1) only 0m, 10m and the bottom.
P.S. the database is huge with a lot of variables
Here is the current code:
Private Sub Azuriraj_Fds_Click()
Set db = CurrentDb
Dim sest As DAO.Recordset
Set sest = CurrentDb.OpenRecordset("FdSest1")
Dim hidd As DAO.Recordset
Set hidd = CurrentDb.OpenRecordset("Hidd1")
sest_stat = sest!Stat
sest_date = sest!Date
sest_time = sest!Time
sest_dep = sest!Dephwl
hidd_stat = hidd!Stat
hidd_date = hidd!Date
hidd_time = hidd!Time
hidd_dep = hidd!Dephwl
sest.MoveFirst
Do Until sest.EOF And hidd.EOF
If sest_stat = hidd_stat And sest_date = hidd_date And sest_time = hidd_time And sest_dep = hidd_dep Then
hidd.Edit
hidd!Ismc.Value = sest!Ismc.Value
hidd!Tsmc.Value = sest!Tsmc.Value
hidd.Update
End If
sest.MoveNext
hidd.MoveNext
Loop
MsgBox "gotovo"
Set sest = Nothing
Set hidd = Nothing
Set db = Nothing
End Sub
For this to work, the tables must have identical scema and be ordered, for example:
Set sest = CurrentDb.OpenRecordset("Select * From FdSest1 Order By sest_stat, sest_dep, sest_date, sest_time")
Set hidd = CurrentDb.OpenRecordset("Select * From Hidd1 Order By hidd_stat, hidd_dep, hidd_date, hidd_time")
I'm trying to iterate through the result set generated with the query that has 2 parameters. Values for these parameters are read from the form fields (start and end date). Since Access throws Run-time error '3061'. Too few parameters. Expected 2. even if the value is set in the form fields, I tried to set the parameters through VBA with QueryDef object (given code below).
It worked OK when start and end date are the same, but if I select different start and end date it won't apply the date filter assigned to the query parameters.
I've tried both to change format of the date values and to cast them to another type, but I've had no success.
Has anyone experienced a similar problem?
Any help would be appreciated!
Query:
SELECT DISTINCT
tblComp_Payout.Agent_ID_int As [Agent ID],
tblExchOffices.Agent_Name AS Name
FROM
tblExchOffices
INNER JOIN
tblComp_Payout ON tblExchOffices.Agent_ID_int = tblComp_Payout.Agent_ID_int
WHERE
((DateValue([Paid_Date])) >= ([forms]![frmReporting]![txtDateFrom])
AND (DateValue([Paid_Date]))<=[forms]![frmReporting]![txtDateTo])
UNION
SELECT DISTINCT
tblComp_Sending.Agent_ID_int AS [Agent ID],
tblExchOffices.Agent_Name AS Name
FROM
tblExchOffices
INNER JOIN
tblComp_Sending ON tblExchOffices.Agent_ID_int = tblComp_Sending.Agent_ID_int
WHERE
((DateValue([Sending_Date])) >= ([forms]![frmReporting]![txtDateFrom])
AND (DateValue([Sending_Date]))<=[forms]![frmReporting]![txtDateTo]);
Method:
Private Sub iterate_Click()
On Error GoTo iterate_Err
Dim rs As DAO.Recordset
Dim qdf As DAO.QueryDef
Set qdf = CurrentDb.QueryDefs("queAgentByDate")
qdf.Parameters.Refresh
If CurrentProject.AllForms("frmReporting").IsLoaded Then
qdf.Parameters("[forms]![frmReporting]![txtDateFrom]") = CStr([Forms]![frmReporting]![txtDateFrom])
qdf.Parameters("[forms]![frmReporting]![txtDateTo]") = CStr([Forms]![frmReporting]![txtDateTo])
Else
Beep
Resume iterate_Exit
End If
Set rs = qdf.OpenRecordset(dbOpenDynaset, dbSeeChanges)
MsgBox rs.RecordCount
If rs.EOF Then Exit Sub
With rs
Do Until .EOF
'Loop logic
Loop
End With
rs.Close
Set rs = Nothing
iterate_Exit:
Exit Sub
iterate_Err:
MsgBox Error$
Resume iterate_Exit
End Sub
First, specify your parameters:
PARAMETERS
[forms]![frmReporting]![txtDateFrom] DateTime,
[forms]![frmReporting]![txtDateTo] DateTime;
SELECT DISTINCT tblComp_Payout.Agent_ID_int As [Agent ID],
tblExchOffices.Agent_Name AS Name
FROM tblExchOffices INNER JOIN tblComp_Payout
ON tblExchOffices.Agent_ID_int = tblComp_Payout.Agent_ID_int
WHERE ((DateValue([Paid_Date]))>=([forms]![frmReporting]![txtDateFrom])
And (DateValue([Paid_Date]))<=[forms]![frmReporting]![txtDateTo])
UNION
SELECT DISTINCT tblComp_Sending.Agent_ID_int As [Agent ID],
tblExchOffices.Agent_Name AS Name
FROM tblExchOffices INNER JOIN tblComp_Sending
ON tblExchOffices.Agent_ID_int = tblComp_Sending.Agent_ID_int
WHERE ((DateValue([Sending_Date]))>=([forms]![frmReporting]![txtDateFrom])
And (DateValue([Sending_Date]))<=[forms]![frmReporting]![txtDateTo]);
Then set their values as true date values:
qdf.Parameters("[forms]![frmReporting]![txtDateFrom]") = [Forms]![frmReporting]![txtDateFrom]
qdf.Parameters("[forms]![frmReporting]![txtDateTo]") = [Forms]![frmReporting]![txtDateTo]
To simplify, rename your parameters to, say, DateFrom and DateTo.
I have a problem to search result from a result set.
I search order based on customer id and store in rs, then I want to search payment based on order id from rs and store in rs2.
This rs2 will then bind in datagridview.
But I found that rs2 will keep reassign the previous data once new data found, so the final result always show the one result which is the last result found. I try use Static rs2 but it still not work.
Hope some expert can provide solution. Thank you.
Private Sub BindCard()
Dim name As String = cboCreditType.Text
Dim db As New ConcertDataContext()
Dim rs = From o In db.orders Where (o.customer_id = MemberLogin.id)
Dim allOrders = From id In rs Select id.order_id
Dim rs2 = From o In db.payments
Where (name = "All" Or o.creditType = name) And
allOrders.Contains(o.order_id)
Select o.payment_id, o.total_payment, o.creditNumber, o.creditType, o.order_id
dgv.DataSource = rs2 'Error occurs here'
End Sub
You have correctly identified the problem in your code, the solution is pretty simple.
Remove the loop, extract a list of orderid to process and then use that list as a condition with Contains against the payments data
' First extract a list of all orders ID present in the orders selected
Dim allOrders = from id in rs select id.orderid
' Then use that list and select only the payments for those orders
rs2 = From o In db.payments
Where (name = "All" Or o.creditType = name) And
(allOrders.Contains(o.order_id))
Select o.payment_id, o.total_payment, o.creditNumber, o.creditType, o.order_id
If you don't need all the data in rs then you can replace the initial query with
Dim allOrders = From o In db.orders
Where (o.customer_id = MemberLogin.id)
Select o.order_Id
So I have a form where I can select an excel file, it'll make a table which is an exact copy of that file, and then it'll try to match fields from that table with a project table and update the matching fields. The issue is sometimes the projects field won't update. As an example the existing value is 1.0319. If my excel file has 1.026 it will not update. 1.026 does appear in the temp table. But if I change it to 1.016 in the excel it will update. Then if I change it back to 1.026, it will update. However if I change it to 1.0319, the original value, it won't update. It honestly has me baffled and I wonder if it's actually a fault in access or VB. Here's the code, I simplified it a bit by removing the other fields it tests for and the excel load as that works fine.
Dim sSQL As String
Dim db As Database
Dim recTemp, recProj As Recordset
Dim intUpdatedRecordCount As Integer
Dim bUpdatedRecord As Boolean
Dim sSelectedFieldsQuery As String
sSelectedFieldsQuery = "P_Ratio"
'Update Generator data with imported table
Set db = CurrentDb()
sSQL = "SELECT TempImpProjRes.Desc, TempImpProjRes.ElemName, TempImpProjRes.BusA, TempImpProjRes.ID, TempImpProjRes.ProjID, " & _
"TempImpProjRes.ElemID, " & sSelectedFieldsQuery & " FROM TempImpProjRes"
Set recTemp = db.OpenRecordset(sSQL, dbOpenDynaset, dbConsistent)
'begin to loop over imported data
If recTemp.RecordCount > 0 Then
recTemp.MoveFirst
Do While Not recTemp.EOF
sSQL = "SELECT Projects.ProjID, Projects.ElemID,"Projects.P_Ratio FROM Projects WHERE Projects.ProjID=" & recTemp!ProjID & " AND Projects.ElemID=" & recTemp!ElemID"
Set recProj = db.OpenRecordset(sSQL, dbOpenDynaset, dbConsistent)
intUpdatedRecordCount = 0
bUpdatedRecord = False
bUpdatedRecord = Not CDbl(Format(recProj!P_Ratio, "0.00")) = CDbl(Format(recTemp!P_Ratio, "0.00"))
intUpdatedRecordCount = intUpdatedRecordCount + BooleanToInt(bUpdatedRecord)
'if any field has been updated then we need to update the respective value in the Projects table
If intUpdatedRecordCount > 0 Then
recProj.Edit
recProj!P_Ratio = CDbl(Format(recTemp!P_Ratio, "0.0000"))
recProj!Updated = Date
recProj.Update
End If
recProj.Close
Set recProj = Nothing
recTemp.MoveNext
Loop
End If
recTemp.Close
db.Close
Set recTemp = Nothing
Set db = Nothing
If I run a query to select three seperate fields from each record. Is it possible to get the results for each returned in three separate strings (one for each field) so that I can cycle through them in vba code?
Yes, you can try opening a Recordset and accessing the field values like a collection.
Dim d As DAO.Database
Dim r As DAO.Recordset
Set d = CurrentDb()
Set r = d.OpenRecordset("SQL or TableName")
While Not r.EOF
Debug.Print r!Field1, r!Field2, r!Field3
r.MoveNext
Wend
r.Close
Set r = Nothing
Set d = Nothing