Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Set current month on Combobox

Status
Not open for further replies.

Costefran

Technical User
Jan 2, 2008
197
GB
Can anyone help with the following
I have an unbound combobox called 'current month' with a value list of each of the 12 months. I would like to set the combobox to display the current month when the form opens as a name (not a number) so have set the format of the combobox to mmmm
However when I select a different month I get the message saying the text you entered isn't in the list even though the combobox limit to list is set to no

Also, I will have another unbound combobox called current year in which the user selects the year and would like to set the value of another text box to the first day of the month and year selected from the two comboboxes on an after update event

Many thanks
 
For your first problem -

In the Form_Load event of the form, put:

Me![Current Month].DefaultValue = Format (date(), "Mmmm")

for your second problem, something like the following will show you how to approach this:

Code:
Public Sub CurrentYear_AfterUpdate()
  ' set textbox to first day of month and year selected
  ' note if you use US date format (yy/mm/dd) you need to switch over the order of month and day.
  Me.txtSomeTextbox = "01/" & me![Current Month] & "/" & me.cboYear
 ' this uses the formatting on the txtSometextbox to display in a more conventional format.
End Sub

John
 
John

I have tried your suggestion in two ways

The first being this

Private Sub Form_Load()
Me.Current_Month.DefaultValue = Format(Date, "Mmmm")
Me.Current_Month = Month(Now())
End Sub

The combobox shows the number 4

The second as this
Private Sub Form_Load()
Me.Current_Month = Month(Now())Me.Current_Month.DefaultValue = Format(Date, "Mmmm")
End Sub

The comboxbox shows nothing

I guess I'm still doing something wrong somewhere?

Thanks
 
Costefan,
You wrote "a value list of each of the 12 months". Could you take a couple seconds and copy the Row Source in a reply? It isn't clear whether you have 1;2;3;4... or "January","February";...

Your additional comment "set the format of the combobox to mmmm" makes no sense unless the value is a date data type.

Typically, I would never use the month names since the names have only visual value and nothing else.

Duane
Hook'D on Access
MS Access MVP
 
Here is my value list

January;February;March;April;May;June;July;August;September;October;November;December

My second comment has a copy and paste error so apologies

I currently have the following in the forms on load event
Private Sub Form_Load()
Me![Current Month].DefaultValue = Format(Date, "Mmmm")
Me.Current_Month = Month(Now())
Me.Current_Year = Year(Now())
Me.FirstDate = "01/" & Me![Current Month] & "/" & Me.Current_Year
End Sub
 
This section doesn't make sense. I don't know if these are the same control since Access might replace the spaces with underscores. You should avoid all spaces in object and field names.
Code:
Me![Current Month].DefaultValue = Format(Date, "Mmmm")
Me.Current_Month = Month(Now())
You are first setting the default to a string and then the value to a number.

I don't know if the combo box is bound to a field or not and if you expect the user to change the value or is it simply displayed.

Duane
Hook'D on Access
MS Access MVP
 
The combobox is unbound

Perhaps I'm doing this in completely the worng way so thought that perhaps I'd explain what I am trying to do

I would like the user to be able to select a month from an unbound combobox but when the form opens I would like the combobox to display the current month


 
It's being used to set the value of a text box called first date and then run a calend as per faq 702 - 838
 
Why not just use one line of code to set the value of FirstDate to the beginning of the month.
Code:
Private Sub Form_Load()
   Me.FirstDate = DateAdd("d",-Day(Date)+1,Date)
  [green] 'if you want the Sunday of the week a month starts
   '  as per the FAQ then uncomment the next line[/green]
   'Me.FirstDate = DateAdd("D",-Weekday(Me.FirstDate)+1,Me.FirstDate)[/green]
End Sub
If you want to display the month name of this date then add a text box with a control source of:
=Format([FirstDate],"mmmm")


Duane
Hook'D on Access
MS Access MVP
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top