DataGridView Cell update - vb.net

I'm working in vb.net and trying to update dgv to list a customers name instead of ID. I already have all the functions to get the customer name given the id, so that isn't much of an issue. My issue is that when I load the DataGridView, it will only select the id.
Ideally, I would like to have it pull the Name while loading, but what I've been trying is running this AFTER it's loaded
For Each row As DataGridViewRow In dgvPhones.Rows
Dim customerID As String = ""
Dim customerName As String
Try
customerID = row.Cells("Customer").Value.ToString
Catch ex As Exception
End Try
customerName = CCCustomer.SelectByID(customerID).FirstNameLastName
row.Cells("Customer").Value = customerName
Next
I put a couple of breaks in and checked the IntelliTrace, and it's pulling in the correct values and all, but it's not actually overwriting the value in row.Cells("Customer"). It just keeps the original id number instead.
To make matters MORE confusing, I've done this before with an MEID/ESN Hex to Dec converter in the same exact place, using the exact same method, and it works just fine.
Can anybody see anything wrong with my code? I've tried using various combinations to see if it's failing because of a bad-value in the database, but it works for NULL/0/-1/1/""... so I'm kind of lost.

Alright, here's what I ended up doing.
Since the DataGridView was only pulling in the CustomerID, I created a new column called CustomerName and made the CustomerID hidden.
Then, I ran my code exactly as seen above, but made it so it pulled in from CustomerID, and wrote to CustomerName.
I guess CustomerID only works as an Integer... very strange, I couldn't find a property for the column saying it could only take int's, but it's working now.

Related

MS Access VBA - extract listbox value on a form (using form name.)

This seemed to be working but just stopped and I'm not sure what I changed to cause this.
I have a listbox on a form. (a single select listbox).
To extract the value, I can do me.listboxName.Column(0) and that works perfectly.
However, that's not the code I want to use. (as I will reference it from another form )
Form_myformName.listboxName.Column(0)
is what I had, and it worked and now it's stopped. It still works for similar code on other forms, so I'm not sure what's happened.
If I type in me.name, it tells me correctly that my form name is "myFormName".
If I type in Form_myFormName., it prompts me with the name of my list box so I know I have the names correct. However, if I try to extract the value using:
Form_myformName.listboxName.Column(0)
it gives me a value of Null, despite the listbox having a selected value. (which I can sucessfully extract by using me.listboxname etc)
hopefully that makes sense. anyone know what I'm doing wrong?
Try it this way:
Forms("myformname").listboxName.Column(0)
I hope that helps.

How to allow a combobox to be left blank//skipped on a form

I am relatively new to Access and any SQL and VBA I have picked up is self-taught, so I would really appreciate answers that are not too heavily laden with technical terms...that said, I am having an issue with allowing comboboxes to be left blank on a form if the user chooses not to input data. Also, I am using Access 2016.
Initially the problem I ran into was that if a combobox was entirely skipped and left blank, or if the user selects the combobox and then tries to move on without making a selection from the list, they got the error "You tried to assign the Null value to a variable that is not a Variant data type," and were unable to move on to any other fields on the form or to save the record being entered.
I found this article that details a possible solution, but I can't seem to get it to work. I made an unbound combobox, set the Row Source to:
SELECT EmailID, PersonalEmail FROM EmailT UNION SELECT "<N/A>","<N/A>" FROM EmailT
ORDER BY PersonalEmail;
where PersonalEmail is a field of type short text and the EmailID is an autonumber. I also followed the article's steps for formatting the combobox (column width, etc.) and set it to Limit to List = Yes.
Here is my exact code:
Private Sub Combo62_AfterUpdate()
If Combo62 = "<N/A>" Then
EmailID = Null
Else
EmailID = Combo62
End If
End Sub
Private Sub Form_Current()
If IsNull(EmailID) Then
Combo62 = "<N/A>"
Else
Combo62 = EmailID
End If
End Sub
< N/A> now shows up on my list, but if it is selected I get the error: "The value you entered isn't valid for this field. For example, you may have entered text in a numeric field or a number that is larger than the FieldSize setting permits."
Access's debugger highlights the line:
EmailID = Null
but I am not sure what steps I should take now to try and fix this.
I am completely open to other methods of allowing the combobox to be left blank if someone knows of a better way to do this also. For all I know, I am missing something really obvious! Thanks in advance for any insight!
EDIT: Thanks for your help guys, I never did figure out what exactly the problem was, but I got some advice from another forum to rethink my database design so this ended up being a null issue--it's all totally different now!
If the recordsource for your form contains a query with one-to-many relationships this error may come up. Try to use only the base table as the recordsource for the form. Use subforms if you need to display related data. The rest of the code may then be unnecessary.

Search functionality on form not working properly

I have a form Main with a subform Issue on it. To implement search functionality on Main so that users can search for records in Issue that have a given substring, Main has a text box keyword and a submit button SubmitBtn. Here is the VBA code I am using to try to make this work:
Private Sub SubmitBtn_Click()
Dim keyword As String
Dim recordSourceSql As String
keyword = Nz(Me.keyword.value)
recordSourceSql = "select * from [Issue] where [Details] like " & quoteWrap(keyword)
Me.Issue.Form.RecordSource = recordSourceSql
Me.Issue.Form.Requery
End Sub
Private Function quoteWrap(value As String) As String
quoteWrap = "'*" & value & "*' "
End Function
The problem is that after this line:
Me.Issue.Form.RecordSource = recordSourceSql
there is only one record showing in Issue--it's the first record in the original recordset, when there should be at least 20 records showing with the value of keyword that I tested. Once this occurs, the Me.Issue.Form.Requery call does not change the contents of Issue.
I know that the correct recordSourceSQL is being created, because when I put in, e.g., "data" for keyword, I get this string for recordSourceSQL:
select * from [Issue] where [Details] like '*data*'
and when I create a query in Access and set this as the SQL, I get all the correct results returned.
What's wrong with this code to search the subform according to the given criteria?
UPDATE: I was able to get this to work by setting Me.Issue.Form.Filter to the WHERE clause in recordSourceSql. I don't understand why .Filter works but changing .RecordSource doesn't.
UPDATE 2: The .Filter solution is not working either. I've described this issue in this SO question.
I created a sample, copied your code and was able to replicate your problem and solve it.
If your subform was unbound (i.e., recordsource was empty) and the primary key of your main table and Issue table had the same field name (e.g., both were named "ID", etc.) and you did not link any parent or child fields, then it will only work if you specifically name each field like this:
recordSourceSql = "SELECT Issue.IssueID, Issue.Details FROM Issue WHERE Issue.Details Like '*Data*'"
You are correct! Wildcards in the select don't work in this scenario, including Select Issue.* From Issue.
Alternatively, if you rename the primary key in your Issue table so it is not the same as your main form main table, then the wildcards work as you would expect. When I made this change your exact code worked:
recordSourceSql = "SELECT * FROM Issue WHERE Details Like " & quoteWrap(Keyword)
Note the Me.Issue.Form.Requery is not necessary. Just setting or changing the RecordSource automatically requeries.
It does not seem to matter if the two primary key fields are dragged to the main or subform. It also doesn't help to make the subform databound but empty with an initial recordsource in the property sheet of "Select * from Issue Where 1=2" (one way to create an empty recordset, but keep the form bound).
I don't know if this is an MS-Access quirk or something intentional. It seems to me that I must have come across this scenario many times (primary key was "ID", subform unbound, no child fields linked) but I don't recall coming across this limitation. Maybe I didn't use a wildcard. When I googled, I didn't find this reported by others but no doubt some MS-Access expert out there will know the reason.
Hope this helps.

Combining a variable and part of a control in VBA

I have a subform which could be different depending on a user choice (as they are make table queries). Consequently I have had to store the actual table name in a variable caled MyResultSO. This works fine and returns the correct table name. I am then trying to get the value of a field from the subform. I am then trying to combine the variable with the field name.text to get the value of the field from the subform but can't get the syntax correct.
I have tried variations but, for example here is one:
MyResultSO = Me.RESULTS.SourceObject
'this works fine and returns "Table.POWER PRICES Query Table 3"
MyProductCode = MyResultSO & !PRODUCT_CODE.Text
'which is where I am trying to combine the value of variable MyResultSO and the field which is PRODUCT_CODE.Text.
Combined this should return the value of the current record from the following:
Table.POWER PRICES Query Table 3!PRODUCT_CODE.Text.
As you will probably tell from the above, I am not very experienced with VB.
Somewhere in the private module of the main form, here Me:
MyResultSO = Me.RESULTS.SourceObject
MyProductCode = Me.RESULTS.Form.Controls("PRODUCT_CODE").Value
Not to use .Text, that requires .Focus: so is bad, it does not always work!
MyProductCode = Me.RESULTS.Form.Controls("PRODUCT_CODE").Text ' <= it's bad.
Here PRODUCT_CODE is the name of a control on the subform named "Table.POWER PRICES Query Table 3".

getting value from other column besides the valuemember column in Infragistics ultracombo

I am fairly new to using infragistics controls (started yesterday). While they are (very) impressive they do add another layer of complexity, which I am muddeling through. So I am asking what I feel to be a fairly simple issue:
I am trying to get the value from another column besides the one that is displayed in the combobox. So far all my attempts have only grabbed the value of the header column not the value in the row column that was selected.
Specifically I want to take from my ultracombobox the value from the lastname column when a row is selected and place it in a textbox.
The below code I have so far retruns the header column (LastName) and nothing else no matter which row I select.
Private Sub ucboPatientInfo_RowSelected(ByVal sender As Object, ByVal e As Infragistics.Win.UltraWinGrid.RowSelectedEventArgs) Handles ucboPatientInfo.RowSelected
ucboPatientInfo.ValueMember = "accounts"
LastName = ucboPatientInfo.SelectedRow.Band.Columns(1).ToString
I added the: "ucboPatientInfo.ValueMember = 'accounts'" to help clarify what my code is doing it is not actually in this part of the code.
Please help
Looks like you found a working solution to your own problem. I thought I would just add in some more information and things to consider.
You may want to avoid a hard index refrence to your cell, in case the position changes in the future as you add new data to the grid's datasource. Say you insert another column ahead of lastName, now your .Cells(5) will return incorrect data.
Instead try using the Cells("columnName") for access like this:
LastName = ucboPatientInfo.Cells("LastName").Value
You should also try to use the eventArgs and sender objects within your event and avoid a direct control refrence. So your code could look like this instead:
LastName = e.Row.Cells("LastName").Value.ToString()
Glad to see your workin things out in any case.
found correct combination:
LastName = ucboPatientInfo.Cells(5).Value