Issue using DLookUp function in Access - database

Ok so I have a text box on my main form and I want it to show the quantity of a product chosen from a drop down list.
Now there is a complication, there are 3 stock locations for each product, but this is simplified as I have 3 different levels stored on the same record for a product, with different column names.
So what I need to do is search for the name of the correct column to find the right stock location (again from a drop down).
My stock level column heading is stored in the variable "Branch" which is a string.
Here is my code.
Me.txtSourceDescQty.Value = DLookup(Branch, "[products/stock]", "[Product Code] = " & Me.cmbSource.Value)
This is the error I get. 'Stock Level' is the column header for one of the stock locations, which is stored under the variable "Branch" in the line of code.
cmbSource is the combo box where the product code is selected.

I think Me.cmbSource.Value needs to appear in quotes:
Me.txtSourceDescQty.Value = DLookup(Branch, "[products/stock]", "[Product Code] = '" & Me.cmbSource.Value & "'")

Related

Where is Access form data populating data from

I am currently trying to help a friend out with their invoicing Access database. I have rarely ever used Access and I am having problems figuring out the location of where the form (frmEntry) is pulling its data from. I did not create this setup so I am unsure of how it works. I am trying to figure out where the address information is being pulled from for when a customer is selected in a drop down on a form. I checked the query and it is only pulling the CustomerID and CustomerName, no address. The table does have address fields but none of the customers in the table have any listed, yet there address is populated along with their name in the form.
I do see where there is another form (frmCustomer) that has customer and there addresses but I am not sure if the other form is pulling from here, and if so, why can I not find the addresses in any of the tables or datasheet views?
Any direction would be very much appreciated. My end goal is to obtain the customer information (address etc) so that I can insert it into a new database that I am working on
Your data contains linebreaks and a combobox only shows one line per record.
To show the data you can replace the linebreaks in rowsource.
SELECT Replace([CustomerName],vbCrLf, " ") as CName FROM table
' vbCrLf is the VBA constant for linebreaks (Cr - Carrige Return, Lf - LineFeed)
This is poor database normalization (imagine you want to search for a customer name that is equal to a city, e.g. Paris). Each line should be a separate field in table (and Postcode too). If there is a linefeed for every data (e.g. no street -> empty line), you can just split the data into the new fields.
'Put this code in a module
'Split function
Public function splitCustomerName(ByVal strCustomerName as String, ByVal index as long) as String
Dim arrCustomerName As Variant ' or declare a fixed array if you know the number of lines
arrCustomerName = Split(strCustomername,vbCrLf)
splitCustomerName = arrCustomerName(index)
End Function
The query
UPDATE table SET newCustomerName = splitCustomerName([table].[CustomerName],0)
, newCustomerStreet = splitCustomerName([table].[CustomerName],1)
, newCustomerCity = splitCustomerName([table].[CustomerName],2);
Just create the necessary columns for name, street and city, then run the query.
If you delete the CostumerName column and rename the table (e.g. newTable) you can create a query with the oldname of the table, that behaves like your old table.
SELECT *
, newCustomerName & vbCrLf & newCustomerStreet & vbCrLf & newCustomerCity as CustomerName
FROM newTable

Cross Dependent Cascading Dropdowns in MS Access

My concept is this: There are three columns relating to a definite hierarchy. I would like our users to be able to input into the combo boxes in whichever order they'd like and have the information pulled in the other two combo boxes be reactive to this.
Example: Country / State / (County/Region/District/City) in a tracker; The sql referenced table with source info would be Country_Name, State_Name, County_Name
If one were to put in "Vienna" into County, one would have options for Georgia, Missiouri, etc. under state, and United States and Austria as options for Country (I have no idea the larger provincial structure for Austria to add them to a state field--this is meant as an analogous example).
If one were to put in "Virginia" under state one would get United States as an option for Country, and Various counties as options for County.
The hierarchy would be relatively normal for inputting a country, as that's the natural drill down.
I do understand how to do a cascading (one-way) combo-box. The problem lies with being unable to use a nested Iif in the control source, or being unable to temporarily amend the control source through _AfterUpdate cases--please excuse the pseudocode:
Private Sub State_AfterUpdate()
If Country = "" And County = "" Then
Me.Country.ControlSource = "SELECT Country_Name FROM Natl_Structure WHERE State_Name = " & Forms![Postal]![State]
Me.County.ControlSource = "Select County_Name FROM Natl_Structure WHERE State_Name = " & Forms![Postal]![State] & ";"
Elseif Country <> "" And County = "" Then
Me.County ControlSource = "Select County_Name FROM Natl_Structure WHERE (State_Name = " & Forms![Postal]![State]) AND (Country_Name = " & Forms![Postal]![Country] & ";"
...and flip it for the opposite case. And set up the opposite of the first case for if both were setup (not that it would be necessary at that point, but just to account for all cases). Then apply the same sort of measure to the other two combo boxes.
Any and all help would be appreciated.
You have to use IIF in your control sources to check if there is already a value in the other combo boxes or not.
Me.County.ControlSource = "Select County_Name FROM Natl_Structure WHERE 1=1" &
IIf(IsNull(Forms![Postal]![State]),"", " AND State_Name = " & Forms![Postal]![State]) &
IIf(IsNull(Forms![Postal]![Country]),""," AND Country = " & Forms![Postal]![Country])

Access 2013 ComboBox based on Combobox with Junction Table

Good day. I would like to know why a Parameter Request pops up when executing a query. I have a form with 2 comboboxes where the 2nd one depends on the value in the 1st one. I do know how to do this when it involves 2 tables. I am having trouble when there is a many to many relationship.
Table 1: name - Supply_Sources, fields - Source_ID(pk), SupplySourceName
Table 2: name - Warehouse_Locations, fields - WLocation_ID(pk), Location_Name
Table 3 (junction): name - SupplySource_WarehouseLocation, fields - Supply_Source_ID(pk), Location_In_ID(pk)
On my form frmInventoryReceivedInput I have cboSupplySource and cboWLocation.
I populate cboSupplySource with
SELECT [Supply_Sources].[Source_ID], [Supply_Sources].[SupplySourceName] FROM Supply_Sources;
I am trying to get a drop down list in the cboWLocation based on the value in cboSupplySource. I am wanting to see the location names of where the supplies are placed in the warehouse.
I have a requery in cboSupplySource After Update (with cboWLocation as the control name). The SQL that I have come up with so far is:
SELECT Warehouse_Locations.Location_Name,
SupplySource_WarehouseLocation.Supply_Source_ID,
SupplySource_WarehouseLocation.Location_In_ID
FROM Warehouse_Locations RIGHT JOIN (Supply_Sources LEFT JOIN
SupplySource_WarehouseLocation ON Supply_Sources.Source_ID =
SupplySource_WarehouseLocation.Supply_Source_ID) ON
Warehouse_Locations.WLocation_ID =
SupplySource_WarehouseLocation.Location_In_ID
WHERE (((Warehouse_Locations.Location_Name)=[frmInventoryReceivedInput].[cboSupplySource]));
When it runs, on tabbing out of cboSupplySource, Enter Parameter Value dialogue box pops up, looking for frmInventoryReceivedInput.cboSupplySource input. Nothing I input brings up the correct list in cboWLocation.
Obviously, I do not have the correct select statement. Any help would be appreciated.
For cboWLocation try the recordSource query:
SELECT Warehouse_Locations.Location_Name
FROM Warehouse_Locations INNER JOIN (Supply_Sources INNER JOIN
SupplySource_WarehouseLocation ON Supply_Sources.Source_ID =
SupplySource_WarehouseLocation.Supply_Source_ID) ON
Warehouse_Locations.WLocation_ID =
SupplySource_WarehouseLocation.Location_In_ID
WHERE ((Supply_Sources.SupplySourceName)=([Forms]![frmInventoryReceivedInput].[cboSupplySource]))
Be aware, that the combobox columns have to be set to columncount 1 in this case with appropriate column width, because you said you only want to see the location names. Further, you should be sure that cboWLocation is not bound to a Control Source, to not overwrite anything.
You can apply it in VBA at the cboWLocation Enter Event.
In the following code example, the combobox cboWLocation is only updated, if there is a value in combobox cboSupplySource.
Private Sub cboWLocation_Enter()
If not (isNull(Me!cboSupplySource) Or Me!cboSupplySource.ListIndex = -1) then
Me.cboWLocation.RowSource = strSQL 'Put here the previous mentioned SQLString
End if
End Sub
HINT: It would be better for performance, when you change the bound column in cboSupplySource to the PK SourceID instead of the name. (With two columns in combobox cboSupplySource) Then use this PK to compare in your WHERE statement instead of the name. this is what keys in tables are for.
Edit: In the WHERE statement, maybe you have to put the namecomparison between ' ' because it is a string

MS Access 2010 - make a form populate fields in multiple tables

I am new to access but I have a problem. Take a look at the screenshot of my table relationships. I would like to create a form such that when someone enters the 'Student Code', the student code is populated in all three tables, rather than just one, saving time and confusion as one only needs to type it in once. Thank you everyone in advance. see http://i.stack.imgur.com/bHBqM.png
In the AfterInsert event of the Student Information form, run append VBA queries. With this, each time you add a new Student record, the corresponding student code will be inserted into the other related tables:
DoCmd.RunSQL "INSERT INTO [Parent Information] (Student Code)" _
& " VALUES (" & Forms!yourformname![Student Code] & ");"
DoCmd.RunSQL "INSERT INTO [Student Medical Conditions] (Student Code) " _
& " VALUES (" & Forms!yourformname![Student Code] & ");"
Alternatively, you can save the above queries as stored queries and run them via Macro (Open Query) or VBA (DoCmd.OpenQuery) -both still in AfterInsert form event:
DoCmd.OpenQuery "ParentInformationQ"
DoCmd.OpenQuery "StudentMedicalConditionsQ"
If your subforms are included on main form, add Requery commands to update subforms. With this, you will see blank rows appear in subforms, ready for data edit (NOT entry since you created the records in prior queries):
Forms!yourformname!yourparentinfosubformname.Form.Requery
Forms!yourformname!yourmedicalconditionssubformname.Form.Requery

Muli-Option MS Access Reports

I am very new to MS Access and yet have been working (loosely) on a DB for a while. We have a DB that tracks membership. There is a table with all of the member info in there. When new and current customers are added, or pay for the current year, the info is applied to a 'PaidYear' column. For years now, I have been adding a query listing the current years' members and adding a report that displays the output of the query.
I would like to create a report where I could (using a drop-down maybe) select the active year and other options such as City, Company Name, Phone Number, etc. Is there any way to simply set this up? It has to be easy enough for my replacements to intuitively use. ie:
Member Report for [Choose Year] <-- Dropdown
[City] [Company] [Phone] [Select Option]<--- Extra Options for reporting
I have been playing with it for a while and while I can get the design set up, I can't set up the functionality. Thank you so much!!!
Yup, if you want to filter down your report, you can write a little bit of VBA to open your report with a filter (you don't need to use a parameter query for this. It may be more efficient to do this at the query execution level, but as far as i've noticed, the performance is the same to just run the full query and filter it at runtime of the report open (Access might actually just do this behind the scenes, again, I don't really know.
Anyways, lets get to it. Here's a code snippet that you can kind of use as a starting point and adapt.
Create a button that says 'Run Report', let's call it cmdRunReport
In the On Click event for that button, you will put some code. I'm just writing this up now, so I might have some syntax errors (don't have Access on this PC).
dim multiple as boolean
dim filtering as string
filtering = ""
if me.yearDropdown is not null then
filtering = filtering + "[myYearField] = " & me.yearDropdown
multiple = true
end if
if me.cityDroPDown is not null then
if multiple then
filtering = filtering + "AND [myCityField] = '" & me.cityDropdown & "'"
else
filtering = filtering + " [myCityField] = '" & me.cityDropdown & "'"
set multiple = true
end if
end if
if me.CompanyDropDown is not null then
if multiple then
filtering = filtering + "AND [myCompanyField] = '" & me.CompanyDropdown & "'"
else
filtering = filtering + " [myCompanyField] = '" & me.CompanyDropdown & "'"
set multiple = true
end if
end if
DoCmd.OpenReport "yourReport", acViewPreview, , filtering
This is the basis of what you can do. I may have a couple if syntax errors and concatenated the filtering string incorrectly (untested), but that's where you could start.
In english, it just looks at your form's dropdowns that you use to filter. It checks if they are not null, and then concatenates their values into the "filtering" string. This string is used as a parameter in your OpenReport method.
Hope this helps.

Resources