All elements of an array cannot be accessed from a for loop in monthCalendar Control - Visual Basic - vb.net

I have a bit of an issue here with the monthCalendar Control in Visual Basic. What is happening in the program is I have a calendar and a text box. The user selects a date on the calendar, then types in information into the textbox component, then presses the add button. From there the add button sets up a parallel array that contains the selected date and the added string from the text box as a way to add planner information for each date. Then the date is bolded for each time an addition like this has happened.
From there you are suppose to be able to click each bolded date and view the information you added. The issue is this only works on the last added date. For some reason it will only read the last element added to the array. I have the for loop down below. I do not understand why I cannot select any bolded date and view the text added. I have option strict on so I know no data loss is occurring. Also I know the date is being selected and placed into the string each time a date is selected on the calendar because I am displaying it onto a label each time. What is the problem. Is it some logical error I am not catching or is it something else?
Public Sub MonthCalendar1_DateChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MonthCalendar1.DateChanged
For index As Integer = 0 To intSizeOfSelectedDatesAry - 1
strSelectedDay = CStr(MonthCalendar1.SelectionRange.Start) ' place the selected date into a string
lblError.Text = strSelectedDay
lblError.Visible = True
Dim strAryDate As String = arySelectedDates(index)
If (arySelectedDates(index) = strSelectedDay) Then
lblInstructions.Text = strSelectedDay
lblSelectedDate.Text = aryTextForDates(index)
index = intSizeOfSelectedDatesAry - 1
End If
Next
End Sub

Related

MS Access VBA - First Record In List is Overwritten on Form Close?

I just learning how to create forms in Microsoft Access 2013 and using VBA programming to control but I'm having an issue I don't quite understand.
I have created a form with a List Box where the source originates from a query that contains the following fields the Query Builder in order from left to right:
|ParentNumber|ParentName|ChildNumber|ChildName|
|------------|----------|-----------|---------|
|------------|----------|-----------|---------|
|------------|----------|-----------|---------|
Some fields from the Query are hidden with a Column Width of 0".
I also have 4 Text Box below the List Box corresponding to the appropriate ParentNumber, ParentName, ChildNumber, and ChildName values.
When I select a record in the List Box, it populates the data to the appropriate Text Box.
When the form first loads, the first row in the List Box is selected:
Private Sub Form_Load()
On Error Resume Next
DoCmd.GoToRecord , , acFirst
Me.List = Me.List.ItemData(0)
End Sub
The issue is that if I select a different row, then close the form, and reopen the form, the first row in the List Box is overwritten with the last selected row before the form is closed.
Even if I start out with any other row selected initially when the form opens, the first row is always overwritten by the last selected row when the form is closed.
The following subroutine handles the update of the Text Box data:
Private Sub List_AfterUpdate()
Dim rowIndex As Integer
rowIndex = Me.List.ListIndex
Me.textBox_ParentNumber = Me.List.Column(3, rowIndex)
Me.textBox_ParentName = Me.List.Column(4, rowIndex)
Me.textBox_ChildNumber = Me.List.Column(6, rowIndex)
Me.textBox_ChildName = Me.List.Column(7, rowIndex)
End Sub
I found a somewhat similar problem to mine, but I tried the suggested solution, which didn't seem to work: MS Access - First record in table is overwritten on form close
I'm completely baffled as to what could cause this based on the code above.
What's my issue?
Thanks.
Few things
Private Sub Form_Load()
'remove this so you can see errors.
'On Error Resume Next
' this goes to the first record of the *form*, not the list.
' you might want this, or not.
DoCmd.GoToRecord , , acFirst
' ItemData returns the data in the *bound column* of the list,
' the data from one specific column. the list is set to that
' data every time the form loads. not what you want; remove it.
'Me.List = Me.List.ItemData(0)
End Sub
This should get rid of the problem. What you want to do next is another question.
Your form should be bound to a record set (table or query).
I had same problem, After hours of verification -
In my case - the problem was that the MainForm , And SubForm (table) - was binded to the same recordset. I just Un-Binded data RS from MainForm.

VB.NET - Date Time Picker saves current date by default, even if no date is chosen

I have a date picker named 'paidDateDp' in my form. My configuration is as below
Property Setting:
Format="Custom"
Private Sub paidDateDp_ValueChanged(sender As Object, e As EventArgs) Handles paidDateDp.ValueChanged
paidDateDp.CustomFormat = "dd-MMM-yyyy"
End Sub
While executing, I see the paidDateDp value is blank. Even if I dint choose any value, when I attempt to save the details of the form to MSAccess using my Oledbadapter query, by default, the current date is getting saved against the paidDate column of my access table.
How to save empty date to my access table from my datetime picker, provided no date is selected?
Someone please suggest me.

How to replace label with dates

I am using Visual Basic 2010. My question can best explained by these images.
I would like to know how to make the labels appear as dates depending on what the DateTimePicker is set on. For Example:
This is what the code looks now that I have solved my issue.
Private Sub MonthCalendar1_DateChanged(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DateRangeEventArgs) Handles MonthCalendar1.DateChanged
Me.Label14.Text = CStr(Me.MonthCalendar1.SelectionRange.Start)
Me.Label13.Text = CStr(Me.MonthCalendar1.SelectionRange.Start.AddDays(6))
Me.Label12.Text = CStr(Me.MonthCalendar1.SelectionRange.Start.AddDays(1))
Me.Label11.Text = CStr(Me.MonthCalendar1.SelectionRange.Start.AddDays(2))
Me.Label10.Text = CStr(Me.MonthCalendar1.SelectionRange.Start.AddDays(3))
Me.Label9.Text = CStr(Me.MonthCalendar1.SelectionRange.Start.AddDays(4))
Me.Label15.Text = CStr(Me.MonthCalendar1.SelectionRange.Start.AddDays(5))
End Sub
I don't have Visual Studio installed on this computer, so I can't give you the exact code, so I will try to explain it.
In the code of the event for the DateTimePicker, you have to calculate for each label the difference in days of the week from your date to the date you want to show (Wednesday to Saturday - 3 days (and you have a little error in the images - you have to switch Wednesday with Tuesday), Thursday to Saturday - 2 days, and so on...).
After that, you subtract (or add, if the day is after the chosen date) the referential date with the corresponding value from above, and you get the date needed. Now you only have to show the result in the label.
Found the answer to my own question.
To retrieve a date and display it in a label:
In the File menu, click New Project.
The New Project dialog box appears.
Click Windows Forms Application and then click OK.
Add a Label control to the form, leaving the default name Label1.
Remove the text from the Text property of the Label control.
Add a MonthCalendar control to the form, leaving the default name MonthCalendar1.
Double-click the MonthCalendar control to enter the default event handler in the Code Editor.
In the MonthCalendar1_DateChanged event handler, add the following code to add items to the list:
Me.Label1.Text = CStr(Me.MonthCalendar1.SelectionRange.Start)
Return to Designer view and add a DateTimePicker control to the form, leaving the default name DateTimePicker1.
Double-click the DateTimePicker control to enter the default event handler in the Code Editor.
In the DateTimePicker_ValueChanged event handler, add the following code to add items to the list:
Me.Label1.Text = CStr(Me.DateTimePicker1.Value)
Press F5 to run the program.
When the form appears, click a date in the MonthCalendar control and verify that the date is displayed in the label.
Click the drop-down arrow of the DateTimePicker control and select a date.
The date and time are displayed in the label.

How To Save DataGridView Cell Style to database

I am working with a DataGridView in vb.net and I am trying to work on saving changes that are made in the DataGridView to my Access Database. The problem is, The user can change Individual cell background colors by selecting whatever cells they want and then changing the color. I can't figure out how I can save the individual cell background color so that when the program is run again, the colors show up. When the user clicks on save the color format gets erased. I also don't think I can do this on the Access side of it by adding a color column because there will be multiple colors used in each row and column. Is there a way to save cell style formatting? Here is code...
This is the save button
Private Sub DsfToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles DsfToolStripMenuItem.Click
BaggersTableAdapter.Update(RentalsDataSet.Tables(0))
RentalsDataSet.Tables(0).AcceptChanges()
End Sub
And The button that is clicked to change the color of the selected cells.
Private Sub Button6_Click(sender As System.Object, e As System.EventArgs) Handles Button6.Click
For i As Integer = 0 To DataGridView1.SelectedCells.Count - 1
'Dim a As String = (DataGridView1.SelectedCells(i).RowIndex.ToString)
Dim colIndex As Integer = DataGridView1.SelectedCells(i).ColumnIndex
Dim rowIndex As Integer = DataGridView1.SelectedCells(i).RowIndex
DataGridView1.Item(colIndex, rowIndex).Style.BackColor = Color.LightGreen
Next
End Sub
I would create tables for saving design colors. Something like
grid_style_header (header_id, grid_id, user_id)
grid_style_cells (id, header_id, row, column, color)...
The problems can happen if you allow filtering of the grid. If that happens, your columns will probably remain, but rows may not or they may move up or down.
better solution if you can implement it: grid_style_cells (id, header_id, row_guid (whatever primary key in your database id), column_name, color)
This makes the row identifier absolute so you don't have any problem with filtering. Column names also get rid of column reording problem.
I'm not aware of any in-built solution for this.

Dynamic Textboxes

I have a gridview - GridView1.
I am adding rows to this Gridview1 dyanamically through code.
One of the columns is a dropdownlist that reads from a sqldatasource.
The textboxes, dropdownlists & sql datasources are all arrays.
If I change the value of the dropdownlist, it maintains its state even after the page reloads on any button click event.
This is ok.
However, the values of the textboxes are not maintained.
Say I enter "Hello World" in the textbox & click "Add" button, I want to collect the text value in dropdownlist(which I can read) & the value in textbox(which returns blank.)
Please suggest a method so that on add button click I can retrive the value I had typed in the textbox.
Each textbox has a unique id & I tried using the ID to get its value
eg
protected Sub Add_Click(ByVal sender As Object, ByVal e As System.EventArgs) handles add.click
{
Dim valueinText = gettext(1).text
}
now if I type "Hello World" in textbox: gettext(1),
Reults:
valueinText = ""
Thanks in advance
You will have to reset the values for unbound textboxes that get their value from code when you post back. In your Page Load Event...
If Page.IsPostback Then
'Code that uses dropdownlistAtoZ.SelectedValue
'to fill in correct value for Textboxes.
End If
I was trying to create the same textboxes on every page_load event. But this just erased their values & their client_ID kept changing.
Instead, I tried making all textboxes Shared and created them only once on add click event, added them to a Row (which is again shared) and on page_load if page.postback = true I just added the row to the table again. If you don't do so, the row will not be shown on reload.
This solved my problem(now the values entered in Textbox were not cleared like before).
I accessed the value by classname.textboxname(i).text.
Now the solution seems obvious, but I spent a few days trying to solve this.
Thanks!