Updating Two different tables through one form

You were supposed to show us the debug.print results from the immediate window.

this one?? This is the window that came out after running the code???

1655052748245.png
 
Just a reminder of the objective.

Substituting Date() for Now() is not a solution.

I made a suggestion that avoids having to format the date and so should avoid the issue with the a.m., assuming that is what is causing the problem. I doubt the OP tried it though.


Make a querydef and save it:
Insert Into YourTable (YourDate, YourFK)
Values Forms!YourForm!YourDate, Forms!YourForm!YourFK;

Then run the querydef rather than an SQL String.

Assuming that Forms!YourForm!YourDate is bound to a date/time data type OR if unbound has a format property that indicates the control should hold a date, you don't need to format anything. The internal double precision number will be used so no formatting will get in the way.

Another solution assuming you insist on using embedded SQL Strings would include concatenating a string formatted as yyyy/mm/dd with a space followed by a string formated as Short Time could work since Short Time uses the 24 hour clock and so doesn't use the AM/PM designators.

dim strDate as String
strDate = Format(YourDate, "yyyy/mm/dd") & " " & Format(YourDate, "Short Time")

Then use strDate bracketed with #'s when building the SQL string.


Well since the data will be stored in a table that has been already created, using "Inser Into" command will create a new register, isn't it??

So have to declere StrDate to fix the problem of the dots that are shown, right?
 
The string looks good so can you verify.
All table names are correct
All field names are correct
(double check all spelling)
Fecha_real is a date field
Event_ID is a numeric field
Status_ID is numeric
 
The string looks good so can you verify.
All table names are correct
All field names are correct
(double check all spelling)
Fecha_real is a date field
Event_ID is a numeric field
Status_ID is numeric

I have verified everything and I cannot figure out why this code is failing to execute. I do not get it. 😭 Everything seems to be OK.


1655054824349.png
 
Make a query in SQL view. Post the debug.print results into the query. Try to run it. This may help show you where the problem is.
 
I said to double check your spelling
Evento_ID and Event_ID are not the same.

These are the type of errors that take a second set of eyes.
 
I said to double check your spelling
Evento_ID and Event_ID are not the same.

These are the type of errors that take a second set of eyes.

You are right!!!!!

That was the problem!!! a single letter!!!! Now it works properly!!!!

Thank you very much for your help MajP, June7, Pat Hartman, theDBguy. I really appreciate your help
 
You are right!!!!!

That was the problem!!! a single letter!!!! Now it works properly!!!!

Thank you very much for your help MajP, June7, Pat Hartman, theDBguy. I really appreciate your help
Hi. Glad to hear you got it sorted out. Good luck with your project.
 
Computers are stupid. YOU have to get everything correct for them to understand you.
A human can spot the difference between evento_id and event_id, a computer cannot. Well not yet anyway. My google minis struggle between off and on :(

Get used to this. :)
 
If you want to include the time you can. I use this function most of my time on my dates.

so just modify the update string and replace the format I provided with
SqlDate(me.fechaReal)

Code:
Function SQLDate(varDate As Variant) As String
    'Purpose:    Return a delimited string in the date format used natively by JET SQL.
    'Argument:   A date/time value.
    'Note:       Returns just the date format if the argument has no time component,
    '                or a date/time format if it does.
    'Author:     Allen Browne. allen@allenbrowne.com, June 2006.
    If IsDate(varDate) Then
        If DateValue(varDate) = varDate Then
            SQLDate = Format$(varDate, "\#mm\/dd\/yyyy\#")
        Else
            SQLDate = Format$(varDate, "\#mm\/dd\/yyyy hh\:nn\:ss\#")
        End If
    End If
End Function
 
If you want to include the time you can. I use this function most of my time on my dates.

so just modify the update string and replace the format I provided with
SqlDate(me.fechaReal)

Code:
Function SQLDate(varDate As Variant) As String
    'Purpose:    Return a delimited string in the date format used natively by JET SQL.
    'Argument:   A date/time value.
    'Note:       Returns just the date format if the argument has no time component,
    '                or a date/time format if it does.
    'Author:     Allen Browne. allen@allenbrowne.com, June 2006.
    If IsDate(varDate) Then
        If DateValue(varDate) = varDate Then
            SQLDate = Format$(varDate, "\#mm\/dd\/yyyy\#")
        Else
            SQLDate = Format$(varDate, "\#mm\/dd\/yyyy hh\:nn\:ss\#")
        End If
    End If
End Function

Yeah that would be useful for my program.

And what about inserting a new record on the table GC_Eventos using the last Event_ID created as the reference???

I cant related a previous Event_ID because there is no previous Event_ID. It is a totally new register. So, once you have filled out a form with the new data, when the user clicks on the button, it will create the new register on the first table (GC_Eventos) but at the same time, I would like to create a new register on another table (GC_Fecha) using that new unique ID (Evento_ID) that has been created few moments ago. All of this in the same event (Clicking the button) The second task comes after the first one is done.

Since I cannot use an Unique ID I was thinking of getting the last Unique ID (Evento_ID) using an aggregate function. (MAX) This would get me the last record created on the first table but i cannot use WHERE with aggregate functions and using HAVING might work but I don´t know.
It has not worked.

Using something like this:

Dim SQL as string
SQL = "INSERT INTO GC_Fecha (Evento_ID) SELECT (Evento_ID) FROM GC_Eventos WHERE Evento_ID = MAX(Evento_ID)"
CurrentDB.Execute SQL

But this doesn't work because of the WHERE clause. I tried using HAVING but it did not work as well.

Is it possible to do or do I need to create another independent event??
 
Last edited:
SELECT Max([Evento_ID]) AS NewID FROM GC_Eventos

 

Users who are viewing this thread

Back
Top Bottom