Passing and Viewing Combobox Text in VB - vb.net

I have a program with two forms: Form1 and FocusPRG. On the FocusPRG form I have two comboboxes set to List, one that let's you pick a Start Time and one that let's you pick a Stop Time for a process. Right now all I want to do is have Form1 look at the selected times and write them to a log file but the log file just shows blank lines and not any of the actual text. Here is the code on Form1 I am using:
Dim StartTime As String
Dim StopTime As String
StartTime = FocusPRG.StartTimePicker.SelectedText
StopTime = FocusPRG.StopTimePicker.SelectedText
WriteLog(StartTime)
WriteLog(StopTime)
What am I missing?

The SelectedText property does not do what you think it does. It's not the text of the selected value. Just like in a TextBox, it's the text that the user has highlighted using either the mouse or keyboard. If you want the text displayed in the control then you want the Text property, again, just like a TextBox.

Use this:
Dim StartTime As String
Dim StopTime As String
StartTime = FocusPRG.StartTimePicker.SelectedItem.ToString()
StopTime = FocusPRG.StopTimePicker.SelectedItem.ToString()
WriteLog(StartTime)
WriteLog(StopTime)
That is to say, use the SelectedItem property of the ComboBox instead of the SelectedText property.
If you read the documentation of the two at MSDN, you can easily see the difference:
Definition of SelectedText says:
Gets or sets the text that is selected in the editable portion of a ComboBox.
Definition of SelectedItem says:
Gets or sets currently selected item in the ComboBox.

FocusPRG must be an instance of the form, not just a class name. When creating FocusPRG, do something like:
dim FocusPRG1 as new FocusPRG
FocusPRG1.Show()
Also use Text, not SelectedText to get the user's input.
StartTime = FocusPRG1.StartTimePicker.Text
StopTime = FocusPRG1.StopTimePicker.Text
Also, the question is a little confusing because you say the controls are combo boxes but they are call xxxTimePicker, which suggests a different type of control.

Related

Access VBA - Type Mismatch when working with Combo Boxes (Multi Valued Fields)

I have a form that displays a single record at a time, and allows the record to be edited by displaying it all in Text Boxes and Combo Boxes. Some of these are ComboBoxes based off of lookup fields, where the values are pulled from a preset list (multi-valued fields).
After that, I have a class module with a property defined for each field in the record (a FirstName Property, a LastName Property, an Address Property... you get the idea). I have a function that creates an object from the class, and then takes the values from the form and assigns them to the corresponding property. This works fine for most of the fields, but as soon as it gets to the first Combo Box (multiple selection), it throws a Type Mismatch error. Code I'm using is:
If Me.Issue <> vbNullString Then
ProfileObj.Issue = Me.Issue
End If
'Me.Issue is the combobox on the form - this is in the forms module
'ProfileObj is the class instance
In case you wanted to see the Property in the class module for the ProfileObj object:
Private ProfileIssue As String
'... other variable declarations
Property Get Issue() As String
Issue = ProfileIssue
End Property
Property Let Issue(rData As String)
ProfileIssue = rData
End Property
I've also tried using Me.Issue.Value, Me.Issue.Text, and Me.Issue.Column(0) to refer to the Combo Box, but none of these have worked either. I even tried using CStr(Me.Issue), to no avail. How can I take whatever's displayed int the combo box and assign it to a String variable?
I figured it out...
I needed to read the text from each Combo Box with each box's .Text property. I had tried that inside of the If statements, but not for the actual comparison that the If statement was built on. The working version of the code now reads:
Me.Issue.SetFocus 'You have to set focus in order to read the text, dont ask me why
If Me.Issue.Text <> vbNullString Then 'This is where my code wasn't working
.Issue = Me.Issue.Text 'I had tried it here before, but the code never got there since the line before failed
End If

VB.net checking if active control is specific DGV

I need to check if my Current DGV is a specific datagridview in my program named DgvDiameterData, so how do I preform this check?
I got this code which sets the active current DGV which works but the check doesnt and no idea how to get it to work
Dim CurrentDGV As DataGridView = ActiveControl
If CurrentDGV = DgvDiameterData Then 'the part after the = doesnt work
End If
Have a look at the Is Operator.
From the link:
The Is operator determines if two object references refer to the same object
So you need:
If CurrentDGV Is DgvDiameterData Then
This is less foolproof but you could also check the Name property.
If CurrentDGV.Name = DgvDiameterData.Name Then

MS Access: How to bound text box of form with query?

I created a form in MS Access 2010 and added a textbox here. Then I created a simple query (for example SELECT 10 AS studval;) and tried to set in Properties (of textbox) -> Data -> Control Source this query, but I got error #Name?.
How do I fix this error?
All names of query, textbox, query return values are correct. Or maybe are there any other ways to bound textbox and custom SQL query?
There is no easy way to do it, but it is possible using the form's On Activate event. First set up a query (Query1) with a single value called "studval" then open the form properties and add an Event Procedure for On Activate. It should look like this:
Private Sub Form_Activate()
Dim myString As String
myString = CurrentDb.QueryDefs("Query1").OpenRecordset.Fields("studval")
Me.Text0.SetFocus
[Text0].Text = myString
End Sub
You need to set the Control Source of the form to the query rather than the control source of the text box. A text box control source can only refer back to it's form's control source.
If you want just one text box bound to a query you have to create a subform linked to the parent form with that text box in it.

Adding a letter to the name of an object

I have a series of pairs text fields that are pragmatically added, one is called 0_1 and the other is called 0_1w. I am wanting todo something to 0_1w when an event happens (keypress) to 0_1, and the same with 0_2, 0_3 etc...
Is it possible to just grab the sender for the keypress event and append the letter w to the end of it or is there any simple way I can do what I need with the w text field from within the other text field.
Thanks
Each control has a Name property, so if you cast the sender to Control, you'll be able to get that name string. You can then append "w" to it and look for another control with that name. You can look up controls by name using the Controls collection on the form.
Dim senderName As String = DirectCast(sender, Control).Name
Dim pairedName As String = senderName & "w"
Dim paired As Control = Me.Controls(pairedName)
However, the form's Controls collection only contains the controls that are directly added to it. If the sender is inside a container control, such as a Panel, only that panel control will be included in the form's Controls collection. In a case like that, you'd need to look at the panel's Controls collection. Therefore, since the two paired controls are probably inside the same container control, it would be safer to do this:
Dim paired As Control = DirectCast(sender, Control).Parent.Controls(pairedName)

Make a button have multiple uses

okay... How do I explain this without being totally confusing?... Alright, I have this form that has MenuScripts (top-levels and second-levels). The problem that I am having is one of the second-levels is "Add" which brings you to another form when clicked. This other form has a button ("Record") and text boxes. This other form allows the user to input data and when the record button is clicked, the inputted data is written into a text file. Ok, so back to the first form. Another second-level MenuScript is "Update" which also brings the user to the other form; but first, the user has to click an item within a listbox to proceed. How do I get the data from the selected item to appear in the appropriate textboxes and how do I get the record button to update data instead of being confused and thinking it is only a add-data button?
Is there a way to use an "if" statement to say something like "if mnuAdd is clicked then" "elseif mnuUpdate is clicked then". Would something like that work for giving the record button multiple uses?
Also, if someone can give me some pointers on making sure the user selects an item within the listbox would definitely be a plus! Thanks, guys!
Unfortunately, I cannot add images since my reputation is too low.
Here is a visual representation of my ultimate goal
Easiest way: before displaying the second form set it's Tag property to something distinct – say "Add" or "Update" – depending on which menu item is selected. Then you just test the Tag value in the button's Click event and proceed accordingly.
As for determining whether a list item is selected: well if there isn't the ListBox's SelectedIndex property will be set to -1.
You need to put a public property on the second form (Details) which specifies which mode it is in. For instance, you could create a mode enumeration like this:
Public Enum EntryModes
AddBook
UpdateBook
End Enum
Then, define a public mode property on the second form, like this:
Public Property EntryMode As EntryModes
Get
Return _entryMode
End Get
Set(ByVal value As EntryMode)
_entryMode = value
End Set
End Property
Private _entryMode As EntryMode
Then, when you show the second form from the menu, just set the property first, before showing it:
Private Sub mnuAdd_Click(sender As Object, e As EventArgs)
Dim dialog As New DetailsDialog()
dialog.EntryMode = EntryModes.AddBook
dialog.ShowDialog()
End Sub
Private Sub mnuUpdate_Click(sender As Object, e As EventArgs)
Dim dialog As New DetailsDialog()
dialog.EntryMode = EntryModes.UpdateBook
dialog.BookToUpdate = ListBox1.SelectedItem
dialog.ShowDialog()
End Sub
As you can see, in the Upate menu click, I also added a line that passes the information for which book should be updated.