Adding i hour to value in combo box (1 Viewer)

CanWest

Registered User.
Local time
Today, 14:41
Joined
Sep 15, 2006
Messages
272
Ok

Thanks to all who helped in my last issue. Never would have dreamed that not having a dedicated id field was the issue

On to my next problem

Same form. I need to take the value from one control box and add 1 hour to it
I think the code should be something like this for a new record

Code:
   If NewRecord Then me.cboEndTime = cboStartTime + 1 hour

These control boxes are set to medium time. I don't know what the expression would be in the code to specify 1 Hour. Obviously what I showed here will not work.
 

CanWest

Registered User.
Local time
Today, 14:41
Joined
Sep 15, 2006
Messages
272

So I tried that and got an error stating the code was incompatible with my version of Access. I am using Access 2016 64 bit

Here is the code, modified for my project

Code:
Dim FirstTime As Date   
Dim IntervalType As String
Dim Number As Integer
IntervalType = "h"
FirstDate = me.cboStartTime
Number = 1
me.cboEndTime =  DateAdd(IntervalType, Number, FirstTime)
 

theDBguy

I’m here to help
Staff member
Local time
Today, 13:41
Joined
Oct 29, 2018
Messages
21,494
Hi. Maybe it’s just a typo but your variable’s name is FirstTime but then you assigned the combo time to FirstDate. To see if the code is compatible with your version, try it out in the Immediate Window using literal or fixed values.
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 16:41
Joined
May 21, 2018
Messages
8,554
I am sure the dateadd function works with every version of access, so that is a strange error message. Something else is going on. You do not need all those variables. I would check however if the combobox is not null and it is a real date.

Code:
if isdate(me.cboStartTime) then
  me.cboEndTime = dateAdd("h",1,me.cboStartTime)
else
  msgbox "Need to select a valid date"
end if

If that does not work you can try adding 1/24 to the starttime. In access dates have a date component (integer) and a time component (decimal). The integer is the number of days since December 31 1899 (the arbitrary base day). The time is the fraction of a day. So .5 is half a day or 12:00 pm. 1 hour is 1/24 of a day.
 

CanWest

Registered User.
Local time
Today, 14:41
Joined
Sep 15, 2006
Messages
272
Hi All

When I added the ID field to the table that inadvertently messed up the controls on the form. Even modifying them to be bound to column 2 did not help. I ended up using this code on the GotFocus event and all is good. That is after deleting and recreating the control
Code:
Dim FirstTime As Date   
Dim IntervalType As String
Dim Number As Integer
IntervalType = "h"
FirstTime = me.cboStartTime
Number = 1
me.cboEndTime =  DateAdd(IntervalType, Number, FirstTime)
 

Uncle Gizmo

Nifty Access Guy
Staff member
Local time
Today, 21:41
Joined
Jul 9, 2003
Messages
16,287
Just a minor observation which might prove useful to you in the future. It was the particular variable in there "Number" which raised a red flag to me!

Code:
Dim FirstTime As Date   
Dim IntervalType As String
Dim Number As Integer
IntervalType = "h"
FirstTime = me.cboStartTime
Number = 1
me.cboEndTime =  DateAdd(IntervalType, Number, FirstTime)


I don't think it is, but "Number" could be a reserved word. The point I am making is that if you use real names, real "Words" as your declared variables, then it is possible that you could fall foul of the reserved words problem, now or in some future version of MS Access. The way this is avoided is by not using a real word. There are various strategies, the most common one is to prefix the variable name with its type so in the case of your "Number" then that would become:- intNumber... And if you apply the same reasoning to the rest of your variables then I would suggest you do something like this:-


Code:
Dim dFirstTime As Date   
Dim strIntervalType As String
Dim intNumber As Integer
strIntervalType = "h"
dFirstTime = me.cboStartTime
intNumber = 1
me.cboEndTime =  DateAdd(strIntervalType, intNumber, dFirstTime)


The approach I've used is termed a "Naming Convention" and the most common one is the Leszynski naming convention
 

Users who are viewing this thread

Top Bottom