How to fix the late key press problem in access - database

I have a textbox that's purpose is to update the subtotal value whenever there is a key stroke in access.
Now the problem with this is that the OnKey press event function updates the subtotal value after 1 extra key press. The text box is named as QuantityOrdSub and it is the box that takes quantity, this text box multiplies it with the unit price (DLOOKUP function which works). The output must instantly go into SubTotalValue text box and it this textbox has a control source = Subtotal
Private Sub QuantityOrdSub_KeyPress(KeyAscii As Integer)
Me.SubTotalValue.Value = (DLookup("UnitPrice", "Stock", "StockID=" & Int(Me.StockSearchID.Value))) *
(Int(Me.QuantityOrdSub.Value))
End Sub
To restate, I am trying to update a text box instantly on key stroke.

Two problems:
1) The correct event to use is On Change, i.e. Private Sub QuantityOrdSub_Change, not KeyPress.
2) .Value of the textbox isn't updated until you leave the control. To get the entered text while the user is typing, you must use .Text, i.e. Me.QuantityOrdSub.Text.

If you want instant calculation, I wouldn't monitor the KeyPress() event. Instead, I would have the control report when its value changes, therefore I'd monitor the Change() event.
Private Sub QuantityOrdSub_Change()
With QuantityOrdSub
If Len(.Text) > 0 Then
SubTotalValue.Value = DLookup("UnitPrice", "Stock", "StockID=" & StockSearchID.Value) * Int(.Text)
End If
End With
End Sub

Related

Do Access recordset fields() need initializing?

I use the onClick event to get values from a combo box. As here:
Me.ComboSelProject.Recordset.Fields(0).Value
On the first onClick event, no matter what row has been selected, the value is from the first row of the recordset. On subsequent onClick events the value is for the selected row.
The value shown in the face of the combobox is always the correct selected value. IOW comboSelProject.text is always correct.
I've tried to initialize the combobox in the Form_load() procedure using:
Me.ComboSelClient.Value = Me.ComboSelClient.ItemData(0) but this has not helped.
Thanks for any help ...
The click event happens before the change event. If you want to use the new value, use the Change event.
And you should probably try to initialize the .text of the combo box rather than the .value

How I can validate ten comboboxes for the same value?

I have 10 comboboxes with players. In each combobox must be selected one player. Now, I must validate their so, that only one unique player can be selected in combobox.
For example:
combobox1 - Anna
combobox2 - Anna (too)
But if Anna is selected I don't want choose she in another combobox. Or I can show error message on click button "Start Game", that Anna is selected in two comboboxes. The main thing it must be validate. I have only one Idea how I ca validate this and it's not the best way.
if cmbPlayer1.SelectedValue = cmbPlayer2.SelectedValue Or
cmbPlayer1.SelectedValue = cmbPlayer2.SelectedValue Or
...
cmbPlayer1.SelectedValue = cmbPlayer10.SelectedValue
and so for each of ten combobox.
How I can do it better?
Create List of all selected value of value.
List<Player> players=new List<Player>
palyers.add(cmbPlayer1.SelectedValue)
upto 10 Players.
Then check unique values
palyers.Distinct().Count()==10
How bout this
I tried with one combobox with bunch of names inside,and one listbox to show 10 players that have selected on the combobox. i make a condition like this :
If you select the same name it will shows an message "Multiple name detected". Else if you didn't do that you'll able to add another name to listbox.
So i make the "prevention" condition when selecting names,not when the button pressed.
Try to add a listbox and combobox,fill the listbox value with names. And make the name appear on listbox when you select it.
And code like this on the combobox
Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
If Not ListBox1.Items.Contains(ComboBox1.Text) Then
ListBox1.Items.Add(ComboBox1.Text)
Else
MsgBox("Multiple name detected")
End If
End Sub
Hope this will help you,and sorry for my bad english.

Handling click events for dynamically created buttons in array

I have an array of buttons that are created through code. The reason why I've done this is because the amount of buttons could change, and in this case, I'm trying to create buttons that are displayed as days in a month on a calendar, and the amount can't be at a set amount because if the amount of buttons were being created for February, the amount would either be 28 or 29.
I've done this how I want to, however the problem comes at having to handle a click event for each button. Since I'm looking for the general idea how to handle a click event in the below example, I want to messagebox what the content is for the button.
Dim btns(Date.DaysInMonth(CurrentYear, CurrentMonth) - 1) As Button
For ButtonCount As Integer To btns.Length
btns(ButtonCount) = New Button With {.Content = ButtonCount}
'Handler goes here.
Next
The way that I reference these buttons individually is through btns(ButtonCount), I do not name them.
So is there a way to add a click event to these buttons created in the example?
Use the AddHandler statement to assign an event handler to your button. In your event handler check which button was pressed.
First of all, take a look at the documentation as suggested in the comments.
(AddHandler Doc.)
Second, you could follow this example to achieve what you want to do:
Dim btns(Date.DaysInMonth(CurrentYear, CurrentMonth) - 1) As Button
For ButtonCount As Integer To btns.Length - 1
btns(ButtonCount) = New Button With {.Content = ButtonCount}
AddHandler btns(ButtonCount).Click, AddressOf OnBtnClick
Next
Private Sub OnBtnClick(sender As Object, e As RoutedEventArgs)
'Your Event Handling
End Sub

How to Populate Label with String Array on ComboBox Item Selection in VB.Net

I have an application in VB.Net where I'm trying to fill a label with some string data that I have in an array. I have a ComboBox which holds some states as the index collection/values. When I select a particular value in the combobox, I want to pull string data from the assigned array, and populate the label with it as a "clickable link a browser window. I'm lost on this, yet here is what I have in my code stub:
Private Sub cboSelectState_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cboSelectState.SelectedIndexChanged
'Create a string array
Dim AlabamaCities() As String = {"http://www.rolltide.com/", "http://www.crimsontidehoops.com/", "http://centralalabamapride.org/"}
Dim strAlabama As String
'Populate label with the array data, on a particular value selection in combo box.
If cboSelectState.SelectedValue("Georgia") Then
strAlabama = CStr(AlabamaCities(3))
lblLinkOutput.Text = strAlabama
End If
End Sub
So when I pick Alabama in my combo box, I want the label to show:
http://www.rolltide.com
http://www.crimsontidehoops.com
http://centralalabamapride.org
The links will be clickable from the label and populate in the same tab whenever clicked. I haven't tried the clickable link part yet, and I will try once I get this down.
I know it's probably bad starting form out the gate. But I'm trying to get the form down to gain the knowledge and plan out a bigger project, and accomplish something better when I think of it. I appreciate your knowledge and assistance.
Firstly, it would make sense to use a Dictionary to store the data. The state names will be the keys and the values would be the arrays of URLs. You would then display the keys in the ComboBox and, when a selection is made, use the selected key to get the corresponding value from the Dictionary.
At that point, you won't be using a Label if you want clickable links. You should use a TableLayoutPanel as a container and then add one LinkLabel to the table for each URL in the array. You can then use a single handler for all the LinkClicked events.

Enter text from text box into combo box vb.net

I have two form, A and B.
On FORM A user will select a country code from combobox and it will then be saved to DB.
On FORM B a textbox shows the country code that was saved to the database earlier.
I want to change the country code in FORM B when edit is selected.
How to change:
1. the textbox will first be hidden
2. a combobox with all the country codes will be shown with selected value equals to the hidden textbox value.
I have tried putting the info into the combobox like the textboxes straight from the database when it is blank, e.g:
cbCountryCode.Text = CStr(dataTable.Rows(0).Item(2))
but this does not work. I also need to keep the country codes in the combobox as the user will need to change the country code if it's wrong. Is there a way of doing this? I have a work around where I don't let the user update the info if the combobox is blank, but I want the country code to be already there so that the user does not have to select a country code again if it is not wrong. Any help with this problem would be greatly appreciated.
EDIT:
datatable.Rows(0).Item(2)
holds a country code, for example, Ireland (+353), United Kingdom (+44) or U.S.A. (1).
This is the code I have for calling the information from the database:
sqlVisitorDetails = "SELECT * FROM visitorDetails WHERE idNumber=#idNumber"
sqlCon.Open()
sqlCmd = New SqlCommand(sqlVisitorDetails, sqlCon)
sqlCmd.Parameters.AddWithValue("#idNumber", txtIdNumber.Text)
dtVisitorDetails = loadDtVisitorDetails()
txtFirstName.Text = CStr(dtVisitorDetails.Rows(0).Item(1))
txtLastName.Text = CStr(dtVisitorDetails.Rows(0).Item(2))
txtContactNumber.Text = CStr(dtVisitorDetails.Rows(0).Item(3))
txtCountryCode.Text = CStr(dtVisitorDetails.Rows(0).Item(4))
txtAddress.Text = CStr(dtVisitorDetails.Rows(0).Item(5))
The country code (e.g. 'Ireland (+353)') is stored in dtVisitorDetails.Rows(0).Item(4) and this is put into the text box txtCountryCode.
When edit is clicked on the form, the text box txtCountryCode is hidden and the combobox cbCountryCode is visible (before edit is clicked txtCountryCode is shown and cbCountryCode is hidden). I then want the country code (in this case 'Ireland (+353)') to be shown in the cbCountryCode combo box. At the moment when the combobox is shown it is blank and the user has to choose a country code again, even if it's right. I hope this makes things clearer.
From the best i can understand from your question.
cbCountryCode.Text = CStr(dataTable.Rows(0).Item(2))
will not work if the DropDownStyle in the properties is set to DropDownList, change it to DropDown instead(if its not).
And to this:
I also need to keep the country codes in the combobox as the user will need to change the country code if it's wrong.
you have to bind the data to the ComboBox to make it work.
EDIT:
If possible, use ColumnName instead of Index for getting a data from datatable. Since you're selecting all record from your database, your may not know when then index would change(when a column is added or deleted from DB)
cbCountryCode.Text = CStr(dataTable.Rows(0).Item("CountryCodeColumn"))
How about that?
cbCountryCode.ClearSelection()
cbCountryCode.SelectedIndex = cbCountryCode.Items.IndexOf(cbCountryCode.Items.FindByText(datatable.Rows(0).Item(2).ToString))
Try this;
cbCountryCode.SelectedItem = CStr(dataTable.Rows(0).Item(2))
The .Text property is the currently selected text in the combo, not the currently selected item.
EDIT: (version 3!)
This definitely works! Create a new project, with a new WinForms project and Form1, etc. Add a button (btnTest) and a combobox (cboTest) and paste this code in;
Private Sub Form1_Load() Handles MyBase.Load
' Add some items
cboTest.Items.Add("U.S.A (+1)")
cboTest.Items.Add("Ireland (+353)")
cboTest.Items.Add("U.K. (+44)")
' Select the first item
cboTest.SelectedIndex = 0
End Sub
Private Sub btnTest_Click() Handles btnTest.Click
' Select the UK entry
cboTest.SelectedIndex = cboTest.FindString("U.K.")
End Sub
And I hate to have to say it, but it works like a charm! I've tested it with the combobox set to DropDown AND to DropDownList and the result is the same. Please check you have the right data going into and out of your routines from the dataTable/Row/Item you are using!!
Try simple:
comboBox.Items.Add("Item 1");

Resources