DLookup When Trying Use Form Field as Criteria - vba

I am new to Access and also to VB. I have a report that displays information on transformers, the report displays data from a query that queries data from two different tables. I would like to use a button to open a PDF document saved in one of the tables.
The code I have so far is this:
Private Sub Command70_Click()
Dim hypa As String
hypa = DLookup("[TestReport]", "TransformerPics", "TxID = " & [Reports]![TransformerInfoSheet]!TXID)
Application.FollowHyperlink [hypa]
End Sub
The error i get is Run-time error '2471' The expression you have entered as a parameter produced this error: 'TP00686'
TP00686 is the transformer number that is displayed on the report.

You need to have some quotes in there:
hypa = DLookup("[TestReport]", "TransformerPics", "TxID = '" & [Reports]![TransformerInfoSheet]!TXID & "'")
Please also see:
DLookup Usage Samples
Dlookup using a string variable as criteria parameter

Related

Code for button on form to update table in ms access

I'm trying to create a simple form using unbound combo boxes and a button that updates a table when clicked. I have already created another form that is similar to this that works great, so I tried to replicate it using the appropriate fields, but I get an error message- Run-time error '3061: Too few parameters. Expected 1.
The only two fields on the form are FrameID and FrameLocation and the button is MoveFrame. The table to be updated is StockFrames.
This is the code I have on the on-click event on the button:
Private Sub MoveFrame_Click()
CurrentDb.Execute "UPDATE StockFrames SET frameLocation = " & Me.frameLocation & _" WHERE FrameID = " & Me.FrameID
Me.frameLocation.Value = Null
Me.FrameID.Value = Null
End Sub
I appreciate any help anyone can offer. I'm very new to MS Access. If there is any other information that you need as far as the property settings, please let me know. I'm eager to learn and to get this form working properly.
Thank you.
If frameLocation is a text field, parameter needs apostrophe delimiters:
='" & Me.frameLocation & "' WHERE.
Remove that underscore character.
Date/Time fields use # delimiter.

Passing a query a parameter [Access 2016]

To make a longer story shorter:
I'm an Access noob, doing a quick-and-dirty conversion of a massive Excel spreadsheet into an Access database. Part of the requirements are to mimic some of the functionality of Excel, specifically, pulling data from a certain table and doing some basic calculations on it (sums, averages, etc.).
I've written a chain of queries to pull the data, count/sum it, etc., and have been testing them by using a manually-entered Parameter (i.e., the kind where the input box pops up and asks you to type a response). Now that I'm ready to drop these queries into a (sub)form, though, I have no idea how to automatically pass that parameter from a box in the form into the subform into the query.
Every query I've written uses a manually-entered Parameter named "MATCHNAME," which holds the name of an individual. In manual testing, if I enter this parameter on one query, all the queries it calls also get that value. So, I think I just need to figure out how to tell the top query what MATCHNAME actually is, and that'll take care of it.
Problem is, I don't know how to do that in Access. If it was any other programming language, I'd do something like "queryXYZ(MATCHNAME);", but I don't think I can do that in Access. Plus, since the values queryXYZ returns are all calculated, I'm not sure how to add an extra MATCHNAME field, nor how to actually make sure that gets read by the queries, nor how to make sure it gets passed down the chain. I've even tried creating a Parameter in design view, then trying to set up Link Master Fields, but the Parameter doesn't appear in that window.
I'd also like to re-run these queries whenever a new record is pulled up, but I'm not sure how to do that either--i.e., the numbers should be current for whatever record I'm looking at.
And, before we go there--I feel like a Relationship is out of the question, as the data itself is auto-generated, and is in rough enough shape to where I can't guarantee that any given key is wholly unique, and large enough (20k+) that, outside of writing a magical script, I can't assign a numerical key. However, I don't know much about Relationships in Access, so please prove me wrong.
(Is this all making sense?)
Do you have any suggestions for me--for how to make a subform read a field on the main form to run its queries on? Alternately, is there an easier way to do this, i.e., to bed SQL calls inside a form?
Thanks very much for your help...
You can use SQL as the recordsource of the subform in the property tab and use the afterupdate event of your matchname field to change yourform.recordsource = "Select * from table where filteredfieldname = & me.matchname & ";" . You can also use sql as the control source of form fields. To pass criteria to filter the subform using the whole table as the recordsource, add an event procedure to your field's after update event like this
`In the declarataions at the top
Global mtchnmfltr as string
Private Sub MATCHNAME_AfterUpdate()
'use the same procedure for Private Sub yourmainform_Current()
mtchnmfltr = "[yourfilterfield] = " & Chr(34) & me.matchname & Chr(34)
'if matchname is not text then just = "[yourfilterfield] = " & me.matchname
with me.subformname.form
.filter = mtchnmfltr
.filteron = true
end with
'Build your sql as a string for your sum avg fields etc. using mtchnmfltr in the where clause
me.yoursumfield.controlsource = "Select...where " & mtchnmfltr & ";"
'etc.
end sub
Or you could throw Matchname into a sql recordsource of the subform and add the function fields to the subform on the same on current and after update events
if me.newrecord = true then
me.dirty = false
end if
me.subform.form.recordsource = "Select Table.Matchname, sum(yourfield) as sumalias, _
(etc.) from yourtable where table.matchname = " & chr(34) & me.matchname & _
chr(34) & Group By table.matchname"
If you are storing your sums etc in a table you need to do it a bit different, since your controls controlsource are bound to fields.
dim strsqlsumfld as string
dim rs as dao.recordset
strsqlsumfld= "Select SUM.....AS sumfldalias where " & mtchnmfltr & ";"
set rs = currentdb.openrecordset(strsqlsumfld)
me.yoursumfield = rs("sumfldalias")
rs.close

MS Access VBA Dlookup on Yes/No field

I can't for the life of me work out what is wrong with this, but I'm not an Access/VBA developer normally..
I have a database table with about 20 fields, one of which is a Yes/No field. I want to extract the Yes/No value using DLookup, however am receiving the following error:
Run-time error '3075':
Syntax error (missing operator) in query expression 'Enabled'.
The code I am using it:
MsgBox (DLookup("Enabled", "Numbers", "ID = " & Me.cbxNumber.Value & ""))
Enabled is a Yes/No field
ID is a String field.
The above DLookup works absolutely fine for returning String values for other fields, so the last parameter with the search query, and the table field, should be fine. It is simply complaining about the return field ('Enabled') thinking it is a query.
The MsgBox element is also irrelevant, as I have tried assigning the result to an Integer and to a Boolean, and it's not complaining of a type mismatch which I would expect if this were the problematic part.
Any thoughts?
You stated that ID is a string field. If that is the case, try changing the DLookup to...
DLookup("[Enabled]", "Numbers", "ID = " & Chr(34) & Me.cbxNumber.Value & Chr(34))
If ID is a Long, then use this string...
DLookup("[Enabled]", "Numbers", "ID = " & Me.cbxNumber.Value)
Your code works fine for me:
Table:
Form:
Code:
Private Sub Command30_Click()
MsgBox (DLookup("Enabled", "Numbers", "ID = " & Me.cbxNumber.Value & ""))
End Sub
The messagebox displays 0 or -1 as required. Things to check:
Is your code in the forms module? Otherwise Me.cbxNumber.Value won't return anything.
What do you get if you run
debug.print Me.cbxNumber
from the OnClick of a button on the form?

DAO Data bind Refresh() call - Run-time error 3061, 3075

I use a DAO Data component to data bind control elements on a form. The query I create dynamically in a recordset which I bind to the Data component. This works pretty well. However, when I run Data.Refresh and the SQL Where statement contains references to the same table via different aliasses then an error shown.
Note: the sql queries below run fine in the MS Access query designer
Global gDB As DataBase
Set Data1.Recordset = GetData(select, from, where, order)
Data1.Refresh
Public Function GetData(select As String, from As String, where As String, order As String) As Recordset
Dim sql As String
sql = "SELECT " & select & " FROM " & from & " WHERE " & where & " ORDER BY " & order
Set GetData = gDB.OpenRecordset(sql, dbOpenDynaset)
End Function
The following will work:
SELECT
WIZ_APPL.*,
TRANS_PRI.Text AS LocalizedText
FROM
TRANSLATIONS AS TRANS_PRI,
WIZ
WHERE
TRANS_PRI.Tag="prog" & WIZ_APPL.Id AND
TRANS_PRI.LanguageId=1 AND
WIZ_APPL.Enabled <> 0
ORDER BY
WIZ_APPL.Id;
Until I try this:
SELECT
WIZ_APPL.*,
TRANS_PRI.Text AS LocalizedText,
TRANS_ALT.Text As AlternativeText
FROM
TRANSLATIONS AS TRANS_PRI,
TRANSLATIONS AS TRANS_ALT,
WIZ_APPL
WHERE
TRANS_PRI.tag="prog" & WIZ_APPL.Id AND
TRANS_ALT.tag="prog" & WIZ_APPL.Id AND
TRANS_PRI.LanguageId=1 AND
TRANS_ALT.LanguageId=2 AND
WIZ_APPL.Enabled <> 0
ORDER BY
WIZ_APPL.Id;
I get a Run-time error '3061':
Too few parameters. Expected 1.
With a slightly different query, which includes an INNER JOIN on another table I get a Run-time error '3075':
Syntax error (missing operator) in query expression 'TRANS_PRI.tag = "prog'.
SELECT
WIZ_APPL.*,
TRANS_PRI.Text AS LocalizedText,
TRANS_ALT.Text As AlternativeText
FROM
TRANSLATIONS AS TRANS_PRI,
TRANSLATIONS AS TRANS_ALT,
WIZ_APPL
INNER JOIN
WIZ_COUNTRY_APPL ON WIZ_APPL.Id = WIZ_COUNTRY_APPL.APPL
WHERE
TRANS_PRI.tag="prog" & WIZ_APPL.Id AND
TRANS_ALT.tag="prog" & WIZ_APPL.Id AND
TRANS_PRI.LanguageId=1 AND
TRANS_ALT.LanguageId=2 AND
WIZ_COUNTRY_APPL.Country=1
ORDER BY
WIZ_APPL.Id;
The bizar thing is that the created recordset is fine and I can print the data. Also, the controls on the form are binding and showing data. However, as soon as I call Data1.Refresh I get the run-time error. Also, refresh() probably destroys the recordset (which is probably normal behaviour).
Set Data1.Recordset = GetData(select, from, where, order)
Do While Not Data1.Recordset.EOF
Debug.Print Data1.Recordset!LocalizedText
Debug.Print Data1.Recordset!AlternativeText
Data1.Recordset.MoveNext
Loop
Data1.Refresh
UPDATE: If I assign the SQL query as String to Data1.RecordSource then the refresh works fine. However, when I assign the SQL query to a recordset then recordset.Name contains only a part of the SQL query. After a Refresh() call Data1.RecordSource is the same as Data1.RecordSet.Name. If the Data component tries to build a query from the shortened Recordset name then it obviously would not work.
Is this a known VB6 DAO Recordset issue?
I am not entirely sure about this but I think this is a DAO recordset and Data component bug.
It looks like the recordset uses the SQL query to create its name property however, it cuts the query short. The data component seems to use this cut query to refresh its internal recordset.
Bug 1: the recordset name property cuts the query short
Bug 2: the data component probably uses the recordset name property to retrieve the SQL query from the recordset or, it uses another property/function which also cuts the query short
Workaround or perhaps the correct method of usage; don't assign the SQL query to the internal recordset, rather use the Data component recordsource property

How to stop Access from thinking my table.field in VBA is a form component?

So I'm writing a SQL query in VBA in Access 2010, and when this code is ran, it thinks that SupplierConnect.MailboxID is a component on a form, where it is in fact a database table (SupplierConnect) and field (MailboxID). Every time that code is ran it pops up a box asking me for input from that form component, which actually isn't one. Is there any way to get around this or code this differently?
Thanks!
' Mailbox ID
If IsNull(MailboxIDComboBox.Value) Then
Else
If firstWhere = True Then
MailboxID = "WHERE SupplierConnect.MailboxID = '" & [Forms]![SupplierQuery]!MailboxIDComboBox.Value & "'"
firstWhere = False
Else
MailboxID = " AND SupplierConnect.MailboxID = '" & [Forms]![SupplierQuery]!MailboxIDComboBox.Value & "'"
End If
End If
This is not popping up asking you for this value because it thinks it is a form control, it is because it is undefined to the query. It cannot find the field within query definition. This usually happens because you do not have the proper case sensitive table or field id or the incorrect table is linked. If you provided more code for the SQL statement you are using from beginning to end and your Exact table structure we can narrow down the mistake.