Insert hours and date in MS access via excel vba - date

I'm trying to insert this datas to MS Access via vba excel but I keep getting an error
dates = Cells(4, "E").Value ' contains 9/11/2013 '
hours= "7:30"
connDB.Execute "INSERT INTO DeptOrder (IDDept, DateIntervention, HourIntervention, bilaterale) VALUES (4, "& #dates# &" , "&hours&" , true)"
This the latest version of my query. The error message is "data type mismatch in criteria expression".
INSERT INTO DeptCommande
(IDDept, IDIntervention, DateIntervention, HeureIntervention, bilatérale, IDChirurgien, NumSalle)
VALUES ('5' , '574', #09-10-2013#, '0.322916666666667', 'True', '1', '4')

Thanks HansUp!!! I did remove all unecessay quotes and it's finally working. Sorry for the long query I wanted to post a short question by adding a few variables only.
VALUES (" & ledept & " , " & lintervention & ", #" & ladate & "#, " & heurevoulue & ", " & bilat & ", " & nomchiru & ", " & numeroSalle & ")"
Debug.Print strInsert
VALUES (5 , 574, #10-11-2014#, 0.322916666666667, True, 1, 4)

Related

Search particular column using WITHIN operator in rewrite query template in Oracle Text

I have one query regarding usage of query template in oracle text.I am using below query but not able to figure out how to restrict it to particular column search , I tried using WITHIN operator but it is not considering it. Any help will be appreciated. Thanks.<query><textquery lang="ENGLISH" grammar="CONTEXT" >' || p_search ||'<progression><seq><rewrite>transform((TOKENS, "{", "}" within contact_name, " ")) </rewrite></seq><seq><rewrite>transform((TOKENS , "{", "}" within contact_name, " NEAR ")) </rewrite></seq><seq><rewrite>transform((TOKENS , "{", "}" within contact_name, "OR")) </rewrite></seq><seq><rewrite>transform((TOKENS , "fuzzy(", ", 50)" within contact_name, " ")) </rewrite></seq></progression></textquery><score datatype="FLOAT" algorithm="COUNT"/></query>
I am trying to use the above query template in Contains operator to fetch the results but I see it is scanning whole document instead of single node.(contact_name in my case) .
I have stored the data in JSON format in my master table.
Thanks.
select * from (select /*+ FIRST_ROWS(20) */ ccc.name
,ccc.value
,ccc.job_title
,ccc.customer_id
,ccc.contact_id
,score(0) score
,score(0) score0
from customer_contacts ccc
right join customers ccu
on ccu.customer_id = ccc.customer_id
where (contains(ccu.search_col_js, '<query>
<textquery lang="ENGLISH" grammar="CONTEXT" >' || :p_search ||'
<progression>
<seq><rewrite>transform((TOKENS, "{", "}" within contact_name, " ")) </rewrite></seq>
<seq><rewrite>transform((TOKENS , "{", "}" within contact_name, " NEAR ")) </rewrite></seq>
<seq><rewrite>transform((TOKENS , "{", "}" within contact_name, "OR")) </rewrite></seq>
<seq><rewrite>transform((TOKENS , "fuzzy(", ", 50)" within contact_name, " ")) </rewrite></seq>
</progression>
</textquery>
<score datatype="FLOAT" algorithm="COUNT"/>
</query>' ,0) > 1)
and :P1_SEARCH_CUSTOMER is not null
order by score desc
);

Me.Filter Access Code Syntax

I am trying to filter a form that emails only the current record. I have tried to do the me.filter command but cannot seem to get the syntax correct. I have provided the code below. The Current Date field is a date field and the Discover, Tail, and FleetID fields are text fields. I was told to put in the me.filter code the primary keys of the table that the form is linked to so the pdf that is produced does not print all the records linked to the form. Please let me know if you see something with my code. Thanks in advance :)
On Error GoTo errhandle
Me.Filter = "CurrentDate= #" & Me!CurrentDate & "#" And "Discover= '" & Me!Discover & "'" And "Tail= '" & Me!Tail & "'" And "FleetID= '" & Me!FleetID & "'"
Me.FilterOn = True
DoCmd.SendObject acSendForm, "frmETIC", acFormatPDF, "email address", "", "", "Recovery Report", "Attached is the submitted Recovery Report"
exitErr:
Exit Sub
errhandle:
If Err.Number <> 2501 Then
MsgBox ("Email cancelled!")
End If
Resume exitEr
All those And-operators are meant to be for the filtering, so they need to be inside the filter-string. Otherwise they are used as boolean operators in VBA, which will cause a type mismatch error when used on strings.
Another issues with your code is the date in the filter string. You will not get the desired result unless the date is formatted properly for use in a SQL criteria.
Replace the line Me.Filter = ... with the following to fix both problems.
Me.Filter = "CurrentDate= #" & Format(Me!CurrentDate, "yyyy\-mm\-dd") & "# AND Discover= '" & Me!Discover & "' AND Tail= '" & Me!Tail & "' AND FleetID= '" & Me!FleetID & "'"
If this filter string does not return the expected results, put a Debug.Print Me.Filter on the next line. This will print the actual filter string into the Immediate Window and allow you to see if it contains the expected values. For further debugging create a new Query in Access, switch to SQL View and enter SELECT * FROM yourTable WHERE filterStringOutput as SQL. Run the query. If it does not return the expected records, remove the criteria one by one to find the one that is causing problems.

Date function in access vba outputting time

I am trying to update a date field in a table to the current date when a button is clicked on a form and that given field is empty. However, when the field is updated, the output is the time 12:00:05, not a date at all. When year(date) is used instead, 7/7/1905 is the output. I am not sure why these values are my outputs. Here is my code:
Dim ctl As Control
Set ctl = [Forms]![frm1]![subfrm1].[Form]![CloseDate]
If IsNull(ctl) Then
DoCmd.SetWarnings False
DoCmd.RunSQL "Update tbl1 SET [CloseItem] = ""YES"" WHERE [ID] = " & [Forms]![frm1]![ID].Value & " AND [Item#] = " & [Forms]![frm1]![subfrm1].[Form]![ItemID].Value
DoCmd.RunSQL "Update tbl1 SET [CloseDate] = " & Date & " WHERE [ID] = " & [Forms]![frm1]![ID].Value & " AND [ItemID] = " & [Forms]![frm1]![subfrm1].[Form]![ItemID].Value
' Output is 12:00:05
' OR DoCmd.RunSQL "Update tbl1 SET [CloseDate] = " & Year(Date) & " WHERE [ID] = " & [Forms]![frm1]![ID].Value & " AND [ItemID] = " & [Forms]![frm1]![subfrm1].[Form]![ItemID].Value
DoCmd.RefreshRecord
' Output is 7/7/1905
End If
How can I get it to output the correct date?
Examine the string your code builds for this segment of the UPDATE statement. (This is an example copied from the Access Immediate window. You can go there with Ctrl+g)
? "Update tbl1 SET [CloseDate] = " & Date & " WHERE [ID] = "
Update tbl1 SET [CloseDate] = 2/16/2015 WHERE [ID] =
The db engine does not see 2/16/2015 as a Date/Time value. Instead, it treats that as 2 divided by 16 divided by 2015. And the resulting number, when expressed as a Date/Time value, gives you this ...
? Format(2/16/2015, "yyyy-m-d hh:nn:ss")
1899-12-30 00:00:05
You can signal the db engine that 2/16/2015 is a Date/Time value by enclosing it in # delimiters like this: #2/16/2015#
However, since the db engine understands the Date() function, you can use that function name directly in your UPDATE statement and not bother about concatenating in a value with proper delimiters.
Dim strUpdate As String
strUpdate = "Update tbl1 SET [CloseDate] = Date() WHERE [ID] = " & _
[Forms]![frm1]![ID].Value & " AND [ItemID] = " & _
[Forms]![frm1]![subfrm1].[Form]![ItemID].Value
Debug.Print strUpdate
CurrentDb.Execute strUpdate, dbFailOnError

MS Chart Control showing blank when rowsource contains a WHERE Clause

First time poster and new to MS ACCESS. I am using MS ACCESS 2002-2003 database format.
I have a Mainform in which I have a tabbed control. In one of the tabs, I have a subform where I put another tabbed control. In one of those tabs, I have a combobox that has a list of categories and I am trying to pass that value to a MS Chart control using VBA. The chart will update (it will show all categories) as long as there is no WHERE clause . If I remove the variable from the WHERE clause and type a VALUE in, it still does not work. When I run Debug.Print strSQLPIE. The strSQLPIE value shows the SQL statement and the WHERE clause correctly. However, Chart keeps showing up as blank. If i copy that SQL statement and put it in a new query, the query runs fine.
I have netted the issue down to the WHERE clause of the SQL statement in the ROWSOURCE property of the chart control. What am i missing?
Here is the code:
Dim DB As Database
Dim rst As Recordset
Dim strSQLSB As String ' subform datasheet sql
Dim strSQLPIE As String ' chart control sql
Dim SelCatStr As String ' combobox for categories
Dim SelYrStr As String ' not being used for now
Dim SelYrMtStr As String ' not being used for now
SelCatStr = Me.PIEChart.Value ' combobox selected value
'SelYrStr = Me.SelectYear.Value ' not being used for now
'SelYrMtStr = Me.SelectYearMonth.Value ' not being used for now
strSQLSB = "SELECT UNIONMASTER.ByMonth, UNIONMASTER.Category, UNIONMASTER.Category1, UNIONMASTER.AdjustedAmount " & _
"FROM UNIONMASTER " & _
"WHERE UNIONMASTER.Category1= '" & SelCatStr & "';" THIS UPDATES THE DATASHEET IN ANOTHER SUBFORM IN THE SAME TAB CORRECTLY ON CHANGE OF COMBOBOX.
strSQLPIE = "SELECT UNIONMASTER.Category1, Sum(UNIONMASTER.AdjustedAmount) AS TotalSpent " & _
"FROM UNIONMASTER " & _
"WHERE UNIONMASTER.Category1= '" & Me.PIEChart.Value & "';"
I TRIED USING SelCatStr and Me.PIEChart.Value in the WHERE CLAUSE and it still down not work. I even tried typing in a value and it still does not work. The debug window shows the SQL correctly and the where clause correctly, but the chart shows up as blank as long as the WHERE clause is there.
Me.PIESubform.Form.RecordSource = strSQLSB
Me.[PIESubform].Form.Requery
Debug.Print strSQLPIE
Me.[CatChart].RowSource = strSQLPIE
Me.[CatChart].Requery
I think the problem has to do with the WHERE clause in the rowsource setting of the CHART. As i said, when I remove the where clause, the chart shows correctly for all categories, the moment i stick the WHERE clause, with a VALUE or with a variable, it shows blank.
The chart works if i use this:
strSQLPIE = "SELECT UNIONMASTER.Category1, Sum(UNIONMASTER.AdjustedAmount) AS TotalSpent " & _
"FROM UNIONMASTER " & _
"GROUP BY UNIONMASTER.Category1 ;"
Me.CatChart.RowSource = strSQLPIE
Me.CatChart.Requery
Any ideas?
You may missing 'GROUP BY' clause in your query. Your query should be like this.
strSQLPIE = "SELECT UNIONMASTER.Category1, " &_
"Sum(UNIONMASTER.AdjustedAmount) AS TotalSpent " & _
"FROM UNIONMASTER " & _
"WHERE UNIONMASTER.Category1= '" & Me.PIEChart.Value & "';" &_
"GROUP BY UNIONMASTER.Category1 ;"

MS-Access 2003: Create multiple records from a single form

Can anyone point me to an example of an Access form which can create multiple records (in a single table) based on one form?
To expand: we're recording information about time spent on a project on a given date. We've had a request for a single form that would allow a user to enter data for 5 (or 7) days of a given week on a single form. He/she would pick a week from a calendar control, a project from a listbox, then enter up to 7 numbers for the hours spent that week.
I did check questions 5294128, which doesn't seem applicable, and question 8070706, which seems to imply that this can only be done in VBA (not using the GUI). Any alternatives?
Thanks.
Something on these lines should suit. This is an unbound form with a subform.
You can get the form type from the form wizard
To work properly, you will need a little code, say:
Private Sub cmdGo_Click()
Dim rs As DAO.Recordset
Dim sSQL As String
Dim sSDate As String
Dim sEDate As String
sSDate = "#" & Format(Me.txtStartDate, "yyyy/mm/dd") & "#"
sEDate = "#" & Format(Me.txtStartDate + Me.txtNoDays, "yyyy/mm/dd") & "#"
sSQL = "SELECT * FROM MyTable WHERE DataDate Between " & sSDate _
& " AND " & sEDate
Set rs = CurrentDb.OpenRecordset(sSQL)
If rs.RecordCount < Me.txtNoDays Then
AddRecords sSDate, sEDate
End If
Me.DataSubform.Form.RecordSource = sSQL
End Sub
Sub AddRecords(sSDate, sEDate)
''Uses counter table with integers from 0 to
''highest required number
''Another useful table is a calendat table, which would
''save some work here.
sSQL = "INSERT INTO MyTable (DataDate) " _
& "SELECT AddDate FROM " _
& "(SELECT " & sSDate _
& " + [counter.ID] AS AddDate " _
& "FROM [Counter] " _
& "WHERE " & sSDate _
& "+ [counter.ID] Between " & sSDate _
& " And " & sEDate & ") a " _
& "WHERE AddDate NOT In (SELECT DataDate FROM MyTable)"
CurrentDb.Execute sSQL, dbFailOnError
End Sub