What I have is a form behind a login and I want to pull in a value from our database for the NameField IF that value exists, otherwise leave it blank for the user to fill in.
So I have the following if statement which works IF there is a value in our database for AliasName:
If _NameID <> Guid.Empty Then
Name.Text = (From c In _Data.CONSTITUENTs
Where c.ID = _NameID
Select c.KEYNAME).FirstOrDefault().ToString()
NameField.Text = (From atc In _Data.CODEs
Join a In _Data.AliasName On atc.ID Equals a.CODEID
Where atc.DESCRIPTION = "Default Name" AndAlso a.CONSTITUENTID = _NameID
Select a.NAME).FirstOrDefault().ToString()
End If
What I now need to know is how to add an if statement to read if there is a value that exists in the AliasName field and the description matches "Default Name", and if so, use that value, otherwise, allow the user to enter their own value.
Right now it is pulling in the value when it exists, but erroring out the page when it does not.
Related
After populating the recordsource the next action is clicking on one of the fields populated to "activate" the record. When clicking this, the goal is that the SEC_ID (A GUID, Number Data Type) is stored as a tempvar and used in future queries. This GUID is also placed in a text box just for a visual debug. However it doesn't put the GUID, it puts random Chinese characters. I've tried to place it into a MsgBox just to also see and it spits out "???????".
My code to populate the rowsource:
Dim componentListSQL As String
If FCSUtilities.AssessmentUoM = "Metric" Then
componentListSQL = "SELECT DISTINCT [100b_Working].SEC_SYS_COMP_ID, [100b_Working].SEC_ID, [110b_RO_Material_Category].MAT_CAT_DESC, [110b_RO_Component_Type].COMP_TYPE_DESC, [110b_RO_Material_Category].MAT_CAT_ID, [110b_RO_Component_Type].COMP_TYPE_ID, [100b_Working].ID_Number, [100b_Working].Model, [100b_Working].Serial_Number, [100b_Working].Capacity, [100b_Working].Manufacturer, [100b_Working].SEC_YEAR_BUILT, ROUND([100b_Working].SEC_QTY, 0) AS SEC_QTY, [100b_Working].UOM_MET_UNIT_ABBR, [100b_Working].UOM_ENG_UNIT_ABBR, [100b_Working].Equipment_Make, [100b_Working].UOM_CONV " _
& "FROM (110b_RO_Units_Conversion INNER JOIN (110b_RO_Component_Type INNER JOIN (110b_RO_Material_Category INNER JOIN 110b_RO_CMC ON [110b_RO_Material_Category].MAT_CAT_ID = [110b_RO_CMC].CMC_MCAT_LINK) ON [110b_RO_Component_Type].COMP_TYPE_ID = [110b_RO_CMC].CMC_CTYPE_LINK) ON [110b_RO_Units_Conversion].UOM_ID = [110b_RO_CMC].CMC_UoM) INNER JOIN 100b_Working ON [110b_RO_CMC].CMC_ID = [100b_Working].SEC_CMC_LINK " _
& "WHERE ((([100b_Working].SEC_SYS_COMP_ID) = [Forms]![200a_MainWindow]![txtDebugCompSysID]) And (([100b_Working].SEC_ID) Is Not Null)) " _
& "ORDER BY [110b_RO_Component_Type].COMP_TYPE_DESC;"
Me![210_ComponentList].Form.RecordSource = componentListSQL
End If
The OnClick event:
Private Sub txtMaterialCategory_Click()
Me.txtActiveSecID.Value = Me.txtSecID.Value
End Sub
The txtSecID appears as a GUID as it should but it's in the txtActiveSecID that it becomes Chinese characters even if I attempt to put it as a tempvar then set it into the txtActiveSecID.
I'm not exactly sure what is going on. Looking at different stacks, it points that it's due to long/memo field but as I said previously, the SEC_ID field data type is Number.
Per MS documentation https://learn.microsoft.com/en-us/office/vba/api/Access.Application.StringFromGUID:
The Microsoft Access database engine stores GUIDs as arrays of type Byte. However, Access can't return Byte data from a control on a form or report. To return the value of a GUID from a control, you must convert it to a string. To convert a GUID to a string, use the StringFromGUID function. To convert a string back to a GUID, use the GUIDFromString function.
StringFromGUID(Me.txtSecID.Value)
However that results in output like:
{guid {2786C27B-CB7C-4DEA-8340-1338532742DE}}
That should still work as filter critera but could do further processing to extract GUID from that string. Use string manipulation functions to remove the {guid header and trailing }. Review Access - GUID value stored in textbox, can't be used in SELECT statements
I am limited in that I have to stick with the old (messy) table design. On this one interface I am developing, there is a combo drop down that is a fixed values list. The options are Group1, Group2, and Group 3. Based on that, a dependent combo needs to be set to the correct row source and control source.
The form is locked, and the user can navigate the records (first, last, next previous, and an unbound combo to jump to a record). During those operations, I just want to refresh which list the dependent combo should pull from, and what value it should bind to, to show the correct value.
When the user clicks to make a "new" record, I won't show the dependent drop down (or make it enabled. haven't decided yet) until they choose a value in the first combo, telling us which group it is.
In order to control this, I created a function for use on this form. This is that function:
Private Function MyGroup()
'Mybitval is a table value that is a bit data type. It means group1
If Me.Mybitval = True Then
Me.cboGroupType.Value = "Group1"
ElseIf Me.Mybitval = False And Nz(Me.MyID1, 0) <> 0 Then
Me.cboGroupType.Value = "Group2"
ElseIf Me.Mybitval = False And Nz(Me.MyID1, 0) = 0 Then
Me.cboGroupType.Value = "Group3"
End If
Select Case Me.cboGroupType
Case "Group1"
Me.cboGroupName.RowSource = "SELECT Group1.ID, Group1.G1Name FROM Group1 ORDER BY Group1.G1Name;"
Me.cboGroupName.ControlSource = me.FKID1
Case "Group2"
Me.cboGroupName.RowSource = "SELECT Group2.ID, Group2.G2Name FROM Group2 ORDER BY Group2.G2Name;"
Me.cboGroupName.ControlSource = Me.FKID2
Case "Group3"
Me.cboGroupName.RowSource = "SELECT Group3.G3ID, Group3.G3Name FROM Group3 ORDER BY Group3.G3Name;"
Me.cboGroupName.ControlSource = me.FKID1
End Select
Debug.Print Me.cboGroupName.RowSource
Debug.Print Me.cboGroupName.ControlSource
Debug.Print Me.cboGroupName.Value
End Function
The idea is that I call this function on load, after I go to the first record, and then on any navigation button click or after update of the go to combo.
The problem is, that the cbogroupname never shows the value it has in the control source. When I try to load the form, my debug rowsource shows the correct sql, and the control source shows the value of the correct table field for the current record it's on, but 2 things don't seem to work. If I don't do the 3rd debug on the value, the form loads fine, but the combo doesn't show the right value, even though it's bound to a table column that has a value. If do debug the value, I get a run-time error '2424': "The expression you entered has a field, control, or property name that [my application] can't find."
When I hit debug, it goes to the last line of the function:
Debug.Print Me.cboGroupName.Value
When I go to save a new record, I run through validation, and having this 1 combo, instead of 3 different ones with alternating the visibility, seems way easier. Is there a reason this isn't working the way I am wanting it to?
Thanks!
And just like that, I figured it out. Of course it takes me all day, and posting this question to get it.
Here is the function, with the right syntax:
Private Function MyGroup()
If Me.Mybitval = True Then
Me.cboGroupType.Value = "Group1"
ElseIf Me.Mybitval = False And Nz(Me.MyID1, 0) <> 0 Then
Me.cboGroupType.Value = "Group2"
ElseIf Me.Mybitval = False And Nz(Me.MyID1, 0) = 0 Then
Me.cboGroupType.Value = "Group3"
End If
Select Case Me.cboGroupType
Case "Group1"
Me.cboGroupName.RowSource = "SELECT Group1.ID, Group1.G1Name FROM Group1 ORDER BY Group1.G1Name;"
Me.cboGroupName.ControlSource = "FKID1"
Case "Group2"
Me.cboGroupName.RowSource = "SELECT Group2.ID, Group2.G2Name FROM Group2 ORDER BY Group2.G2Name;"
Me.cboGroupName.ControlSource = "FKID2"
Case "Group3"
Me.cboGroupName.RowSource = "SELECT Group3.G3ID, Group3.G3Name FROM Group3 ORDER BY Group3.G3Name;"
Me.cboGroupName.ControlSource = "FKID1"
End Select
Debug.Print Me.cboGroupName.RowSource
Debug.Print Me.cboGroupName.ControlSource
Debug.Print Me.cboGroupName.Value
End Function
Provided your form is bound to a table, such as mine is, the control source needs to = "fieldname" . It needs to be the field name in double quotes. Not me.fieldname as that ends up making the controlsource the value of that field name. It also can't be [table1]![field1] I tried that and that doesn't work either.
The above works beautifully. Now to test the rest of the form!
Access 2013 - Reference an Unbound text box on a Form
I am currently trying to use an unbound text box [Text161] on a Form name [DCM_Gap_Servers] to sort information through a table. I want the query that I created to be able to take the users input from [DCM_Gap_Servers]![Text161] as the field that is being sorted from the table names 'Server'.
This is the SQL I am using right now in the query:
SELECT * FROM Servers WHERE "Forms![DCM_Gap_Servers]![Text161]" IS NULL
** I have already Tried:
"Forms![DCM_Gap_Servers]![Text161]" ; (Forms![DCM_Gap_Servers]![Text161]); Forms.[DCM_Gap_Servers]![Text161]
This will work at any time if I replace the Text Box reference with the actual Field name I am using, but since there are hundreds of combinations of fields, I need the reference to work.
I have looked all over, and I can't seem to find the correct answer. I am willing to do it in VBA if needed, whatever it takes to get the filtering done correctly.
Thank You.
It is:
SELECT * FROM Servers WHERE Forms.[DCM_Gap_Servers].[Text161] IS NULL
but that will just select all records whenever your textbox is Null.
So it rather is:
SELECT * FROM Servers WHERE SomeField = Forms.[DCM_Gap_Servers].[Text161]
To use the form value as a field name, you must use concatenated SQL:
strSQL = "SELECT * FROM Servers WHERE " & Forms![DCM_Gap_Servers]![Text161].Value & " IS NULL"
This you might pass to the SQL property of an existing query object:
MyQueryDef.SQL = strSQL
Or:
Constant SQL As String = "SELECT * FROM Servers WHERE {0} IS NULL"
FieldName = Forms![DCM_Gap_Servers]![Text161].Value
MyQueryDef.SQL = Replace(strSQL, "{0}", FieldName)
Of course, take care the the field name isn't a zero length string.
I feel like this should be easy, but I haven't been able to come up with a clear answer for this after searching off and on all day.
I have a users table that has email addresses in it.
I have a combo box that references this table.
All I want to do, is set the email address field of the selected user to a string, so I can then do things with that.
just trying to get a string back from a sql query like :
"SELECT emailAddress FROM tblUsers WHERE id = " & Me.cmbUser.Value & ""
Can someone point me in the right direction here?
You can use Combo Box to return email address after user select user from list.
Assume your user tblUsershas User and Email fields
Set your combo box property:
1.Row Source = SELECT User, Email FROM tblusers
2.Column Count = 2
3.Bound Column = 0 (0 is the first column and 1 is the second column which is email)
4.Column width = x";0" (x is your combox box width)
You can get email address from me.combobox.column(1). Both me.combobox.value and me.comboxbox.column(0) is selected User
If you actually want to execute the query, you have multiple options:
You can use DLookup:
TL;DR:
DLookup("Column", "Table", "Col=Value") will execute SELECT Column FROM Table WHERE Col=Value and return the first row.
For the query from your question, you need to use DLookup like this:
Dim mail As String
mail = Nz(DLookup("emailAddress", "tblUsers", "id = " & Me.cmbUser.Value))
You can load your SQL query with a Recordset.
This makes more sense than DLookup if you need more user data from the table than just the email adress.
Dim mail As String
Dim phone As String
Dim RS As DAO.Recordset
Set RS = CurrentDb.OpenRecordset("SELECT emailAddress, phoneNumber FROM tblUsers WHERE id = " & Me.cmbUser.Value)
If Not RS.EOF Then
mail = Nz(RS("emailAddress"))
phone = Nz(RS("phoneNumber"))
End If
RS.Close
Set RS = Nothing
(in order for this to work, your Access database needs a reference to any version of the Microsoft DAO Object Library...this should already be the case in most newer Access versions)
Note the usage of Nz in both examples - this is necessary if the mail adress can be NULL. Without Nz, the code would crash because of setting a string variable to NULL.
In order to test whether a particular query against an MS Access "database" should be returning any records (it's not), I am running it in Access like so:
SELECT TOP 5 t_accounts.account_no as AccountID, IIF(ISNULL(t_accounts.name),'[blank]',t_accounts.name) AS Name
FROM t_accounts
INNER JOIN td_department_accounts ON (t_accounts.account_no = td_department_accounts.account_no)
WHERE (AccountID >= 1) AND type = 'DE'
The "Top 5" and the "AccountID >= 1" are hardcoded versions of what I'm using in the code:
cmd.CommandText =
string.Format(
#"SELECT TOP {0} t_accounts.account_no as AccountID, IIF(ISNULL(t_accounts.name),'[blank]',t_accounts.name) AS Name
FROM t_accounts
INNER JOIN td_department_accounts ON (t_accounts.account_no = td_department_accounts.account_no)
WHERE (AccountID >= #firstId) AND type = 'DE'", CountToFetch);
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("#firstId", FirstId);
Yet Access prompts me with:
(If I enter "1", it does then return a few records).
What must I do to get Access' query running process to straighten up and fly right? Or is it me that has vertigo?
UPDATE
Perhaps a clue is that with the C# code shown above, I get, "No value given for one or more required parameters." Why? The only parameter is firstId, and it's value is indeed supplied...?!?
UPDATE 2
Even though it works, running Code Analysis on it causes that august tool to wrinkle its brow, scowl, and growl:
CA2100 Review SQL queries for security vulnerabilities The query
string passed to 'OleDbCommand.CommandText.set(string)' in
'NRBQSQLRepository.GetNDepartmentsFromID(string, int)' could contain
the following variables 'CountToFetch'. If any of these variables
could come from user input, consider using a stored procedure or a
parameterized SQL query instead of building the query with string
concatenations.
From Why does Access want me to enter a parameter value?
Access displays the Enter Parameter Value dialog box when you open an
object that contains an identifier or expression that Access cannot
interpret.
In this case AccountID is the field alias for t_accounts.account_no. You tried to reference the field alias in the Where clause. You can't do that.
change
WHERE (AccountID >= 1) AND type = 'DE'
to
WHERE (t_accounts.account_no>= 1) AND type = 'DE'