Display SQL time field on Access form as Access me

2019-02-28 03:53发布

问题:

I am using Access 2010 linked to SQL Server 2008 R2. My problem - how do I display time on an Access form as hh:mm am/pm instead of SQL Server's time(7) format of hh:mm:ss.nnnnnnn?

To complicate things, I do have a look-up table with all the times in half hour time frames? Ex. in SQL, the lookup field says 07:30:00.0000000 - I want to choose a time from the dropdown in my Access form that says 7:30 am and then display it on the Access form as 7:30 am. If I choose it, I want it to store in SQL Server as 07:30:00:0000000 (because I have no other choice).

I can't find anything on this.

回答1:

Access doesn't have a Time data type, so when it links to a SQL Server time column it maps that column as Text. That's why any Date/Time Format specifier you try to apply to the linked table value has no effect: as far as Access is concerned it's a string.

So if we have a SQL Server table [Appointments]:

and we create a Linked Table in Access we get

If we create an Access form for that linked table and use plain old bound controls we get this

The text box for [apptStart] does work, but it doesn't look so great. The workaround is to leave that text box on the form, but hide it (set its .Visible property to No) and then add another unbound text box to the form. (In this case I called it [txtStartTime].) Set the .Format of the new text box to Medium Time and adjust any other formatting as required (e.g., I set Text Align to Left).

In the On Current form event, calculate the Access Date/time value corresponding to the start time in SQL Server table and stuff that value into the [txtStartTime] text box. Now we get something that looks a little better:

Now what if the user wants to edit that value? In the After Update event for the [txtStartTime] text box we create the corresponding SQL Server time string and stuff that value into the (invisible, bound) [apptStart] text box. When the record is saved the new time value is updated in the SQL Server table.

The code behind the form is as follows:

Option Compare Database
Option Explicit

Const accessDate0 = "1899-12-30 " ' <- note trailing space
Dim startTimeAsDateTime As Date

Private Sub Form_AfterUpdate()
    Me.Requery
End Sub

Private Sub Form_Current()
    Me.txtStartTime.Value = CDate(accessDate0 & Left(Me.apptStart, 8))
End Sub

Private Sub txtStartTime_AfterUpdate()
    Me.apptStart.Value = Format(startTimeAsDateTime, "hh:nn:ss")
End Sub

Private Sub txtStartTime_BeforeUpdate(Cancel As Integer)
    On Error GoTo txtStartTime_BeforeUpdate_Error
    startTimeAsDateTime = CDate(accessDate0 & Me.txtStartTime.Value)
    Exit Sub

txtStartTime_BeforeUpdate_Error:
        MsgBox "Start Time appears to be invalid."
        Cancel = True
End Sub


回答2:

Sorry, I don't have a dev environment setup, but can you change your SQL query to:

SELECT Convert(varchar(5), getdate(), 108) 

to give HH:MM and truncate the rest so it displays correctly.

Then, you can cast as date on the way back.