File sharing lock count exceeded, runtime error 3052 - sql

Okay so i have a macro that goes in and checks the table to determine the format of a application that is set by the user. If this format is set to be a European format then it runs a function that will go through a table and swap the date formatting from MM/dd/yyyy to dd/MM/yyyy. everything seems to be set up correctly and when it's in US formatting the macro runs correctly(which it should considering it skips over this line if the formatting is not set to European.) however, whenever the formatting is set to European, i get this run-time error that pops up:
Run-Time error '3052':
File sharing lock count exceeded. Increase MaxLocksPerFile registry entry.
Now i'm always wary whenever i get an error that states the fix is to modify something in the registy. Not to mention this is not something i would want a client to be doing just to get the application to work. However i gave it a shot just to see if that would indeed fix the problem by following these steps. Although when i did modify the default MaxLocksPerFile from it's default(9500) to (30,000) and ran the macro again i still got the same error at a slightly larger count.
Before i would get the error after running through about 12,000 rows, after the change it would come up around 15,0000 rows.
This leads me to believe that i am incorrectly closing out my updates after each row is modified.
This is my code below:
Public Function UKDateFormat() As Variant
Dim varPieces As Variant
Dim strNew As String
Dim varReturn As Variant
Dim Strsql As String
Dim db As dao.Database
Dim rstAlarmdetDateMod As dao.Recordset
Dim i As Long
Set db = CurrentDb()
Strsql = "select AlarmDate From AlarmdetDateMods;"
Set rstAlarmdetDateMod = db.OpenRecordset(Strsql, dbOpenDynaset)
If (rstAlarmdetDateMod.RecordCount > 0) Then
rstAlarmdetDateMod.MoveFirst
i = 0
While (rstAlarmdetDateMod.EOF) = False
i = i + 1
rstAlarmdetDateMod.Edit
rstAlarmdetDateMod![alarmdate] = CDate(Format(rstAlarmdetDateMod![alarmdate], "dd/MM/yyyy"))
rstAlarmdetDateMod.Update
rstAlarmdetDateMod.MoveNext
Wend
End If
rstAlarmdetDateMod.Close
db.Close
End Function
This is my Update query:
UPDATE DISTINCTROW AlarmdetDateMods SET AlarmdetDateMods.AlarmDate = CDate(Format([AlarmDate],"dd/mm/yyyy"));
What i would like to know is: What exactly i am doing wrong with my function that it causes this error to come up and how can i correct it in a way so that i don't have to go into and modify the registry to get this function to work?
Any help or suggestions are greatly appreciated.
Thanks.

In addition to your UKDateFormat function, you showed us this UPDATE statement.
UPDATE DISTINCTROW AlarmdetDateMods
SET AlarmdetDateMods.AlarmDate = CDate(Format([AlarmDate],"dd/mm/yyyy"));
However, it's not clear what's going on with that. Does it work? Does it fail with the same error as the UKDateFormat function? A different error?
Try a revised UPDATE statement. I think DISTINCTROW is not useful in this case; suggest you discard it.
Also your UPDATE seems to rely on implicit data type conversions. AlarmDate is a text value. You pass that text to the Format() function, to treat it as a Date/Time value and transform it to a different formatted string. Then you ask CDate() convert that string value to a Date/Time value. Finally the Date/Time value is stored back to the AlarmDate text field.
At a minimum I would avoid casting the formatted string back to a Date/Time value before storing it in the text field. However, I would also make the data type conversions explicit rather than implicit. And limit the UPDATE attempt to only those rows where AlarmDate holds the text representation of a valid Date/Time value.
UPDATE AlarmdetDateMods
SET AlarmDate = Format(CDate(AlarmDate),"dd/mm/yyyy")
WHERE IsDate(AlarmDate) = True;
Note this suggestion assumes changing all your stored AlarmDate text values is reasonable. I have doubts about that. Seems like maybe AlarmDate should be Date/Time rather than text. And if you need to change the format when displaying those Date/Time values, do it with the format property of a bound control on a form or with the Format() function in a query.

Related

How to Convert from Date Object into a Range to Display at Bookmark in Word VBA?

Context: I am trying to create a document to where the document prompts the user for information. Right now, I just trying to understand the reason I cannot get the date turned into a range to be able to display at the bookmark.
I have tried creating different types of objects to associate with Date but I just do not understand the reason I am getting a compile error.
Sub TodayDate()
Dim dateVariable
dateVariable = Date
Set dateVariable = ActiveDocument.Bookmarks("bmkTodayDate").Range
dateVariable.Text = dateVariable.value
I am wanting to display the date at the posted bookmark location ("bmkTodayDate"), but I keep getting error messages. What is the correct object or the correct code that I should have used?
So I did some searching on the net, and I came up with a solution. Cindy Meister was right that I need to declare some objects into existence. I didn't realized that the bookmark object "bmkDate" needed to come into existence as well.
Sub TodayDate()
Dim bmkDate As Bookmark
Set bmkDate = ActiveDocument.Bookmarks("Date")
bmkDate.Range.Text = Date
Things to note: First, thank you Miles Flatt for your input. What I found that works for me is that "Date" is an actual posted/inserted bookmark within the Word document; the today variable Date is in the default format, so it will display mm/dd/yyyy at the location; and finally, the modifier/suffixes .Range.Text is what really helped. The Range.Text returns or sets the text in the specified range or selection.

Can we format, in VBA, a query field properties after it is create?

In Access, when creating a new query using vba, some of the data I'm getting are currency, but it comes by default as General Number. I would like to change it's format like we can do in the Property Sheet, but I can't seem to find a way.
I have tried
db.QueryDefs("TestQuery").Fields("SumOfSomething").Type = DAO.dbCurrency
But then I get: Run-time error '3219': Invalid operation.
I have searched around a bit and found a similar question (but couldn't find it back) to which the answer was that you can't change that after it is created. I fail to see the point of having the possibility to change the type if we can't actually do it.
So, in the end, can we change (in VBA) the properties of a field after the query is created?
Your question appears to be simple, but as you've probably guessed by now, it isn't.
The format property is a custom property of a field. They can be present, and if they are, they should be changed using the Field.Properties collection, but if they aren't, they need to be created using Field.CreateProperty method and then appended to the Fields.Properties collection
Const currencyFormat As String = "€ #,##0.00;€ #,##0.00-"'Dutch currency format, you need to set this for your locale
Dim qd As DAO.QueryDef
Set qd = CurrentDb.QueryDefs("TestQuery")
On Error Resume Next
qd.Fields("SumOfSomething").Properties!Format = currencyFormat
If Err.Number = 3270 Then 'Property not found, field had no format set
qd.Fields("SumOfSomething").Properties.Append qd.Fields("SumOfSomething").CreateProperty("Format", dbText, currencyFormat)
End If
On Error GoTo 0
You can get the currency format for your locale by setting a field to use it, and then using ?CurrentDb.QueryDefs("TestQuery").Fields("SumOfSomething").Properties!Format
We have to be clear here on the goal. Do you want to “just” cast or change the column type returned in a query, or do you want to modify the actual column type in the database table? These are two VERY different goals.
To use code to modify a column type, you have to create (add) the new column type, transfer the data, and then delete the old column, and then re-name the column back to the old type.
In other words, you can’t just change the data type, since Access will THEN have to update each and every row to the new data type.
The UI system in fact does the above behind the scenes.
If you use DAO code, then you need the following code outlined here:
http://accessblog.net/2007/03/how-to-change-field-type-using-dao.html
However, above is quite a bit of code. In place of DAO code to add the column, you CAN use a DDL command.
(SQL data definition language) to modify the type. Thus in code you can do this:
Dim db As dao.Database
Dim strSQL As String
Set db = CurrentDb
strSQL = "ALTER TABLE dbo_tblHotels1 ALTER COLUMN MyAmount currency"
db.Execute strSQL, dbFailOnError
So in above, we change the column "MyAmount" to currency type.

MS Access - SQL append query behavior is erratic

I've been working on an Access database for the last couple weeks, and it's my first project with the tool. Dealing with append queries seems to have become an utter nightmare, and is incredibly frustrating. Even more so because it seems to have simply stopped working in any consistent manner overnight.
The SQL query that I have written goes thus:
PARAMETERS noteDetails LongText, noteTime DateTime, srcUserID Long;
INSERT INTO tblNotes (NOTE_DETAILS, NOTE_TIME_CREATED, NOTE_SOURCE_USER)
VALUES (noteDetails, noteTime, srcUserID)
In tblNotes:
NOTE_ID is an AutoNumber
NOTE_DETAILS is a Long Text
NOTE_TIME_CREATED is a Date/Time
NOTE_SOURCE_USER is a Number
The way that I'm running this query is through VBA:
Set qdf = CurrentDb.QueryDefs("qerApndNote")
qdf.Parameters(0).Value = txtDetails.Value
qdf.Parameters(1).Value = Now()
qdf.Parameters(2).Value = getCurrentUserID()
qdf.Execute dbFailOnError
qdf.Close
Set qdf = Nothing
' Where CurrUserID is a global long
' txtDetails.Value is a textbox's contents
' Now() is the VBA built-in function to return a date/time combo
I have attempted to run this query manually from the navigation bar, and it works fine when done in that manner.
However, running it from VBA has resulted in such things as there being no time / date inserted, sometimes a user ID is not inserted, sometimes both, sometimes even the details text is missing.
What is it that I'm missing? Is there any general advice for users of MS Access to follow that I am not? I'm aware that NOTE is a restricted word in Access, but I really don't think that should apply here, right?
Thanks in advance!
EDIT: The form that I'm passing data from is called frmNewNote, and there is a control in it named txtDetails. It's just a regular textbox. Don't really know what else to share about that.
The getCurrentUserID function is in a module, modGlobal:
Public CurrUserID As Long
Public Function getCurrentUserID() As Long
getCurrentUserID = CurrUserID
End Function
Public Function setCurrentUserID(CurrID As Long)
CurrUserID = CurrID
End Function
It's about as barebones as you can get, really. And there is never a circumstance that you'll get to the form before SetCurrentUserID has been called during your... session? There's a login form involved.
#Andre's code for logging:
0 noteDetailsText This is a note test
1 noteTimeCreated 9/6/2017 10:28:45 AM
2 srcUserID 1
As for my architecture, um, it's just the single database file right now, on the desktop. The entire function/sub is run when you click a button, btnEnter. It does some other stuff before it gets to the SQL statement bit - checks for null values and prompts user for entries if that's the case.
I just remembered something:
MS Access 2013 calling insert queries from VBA with strange errors
You have a LongText parameter. These don't really work. See also https://stackoverflow.com/a/37052403/3820271
If the entered notes will always be <= 255 characters, change the parameter to ShortText.
If the text can be longer, you'll have to use either SunKnight0's approach with a concatenated INSERT statement.
Or use a Recordset and its .AddNew method, which will be a similar amount of code to your current solution, but also be completely safe from injection or formatting issues.
You are doing way more work than you have to. All you need is:
DoCmd.RunSQL("INSERT INTO tblNotes (NOTE_DETAILS, NOTE_TIME_CREATED, NOTE_SOURCE_USER) VALUES ('" & Me.txtDetails & "',Now()," & CurrUserID & ")")
Note the change from txtDetails.Value to Me.txtDetails which is what may have been messing you up. This of course assumes the code runs in the form's context, otherwise you have to get he value of the text field using a reference to the form.
The only other thing to consider is making sure Me.txtDetails does not have any single quotes, so probably use Replace(Me.txtDetails,"'","''") instead.
That way you can also replace DoCmd.RunSQL with MsgBox to troubleshoot the exact query.

Problems converting string to DateTime

I am making a program in which there is a function that check the database for user that haven't been called for 2 weeks or more, and shows them in a ListView.
In this part I am checking how long ago they were called:
Dim r As Int32 = excelWS.UsedRange.Rows.Count
Dim bel As New ArrayList
For nm As Int32 = 1 To r Step 1
If Convert.ToInt32(DateDiff("ww", Date.ParseExact(excelWS.Cells(nm, 1).value(), "yyMMddhhmm", System.Globalization.DateTimeFormatInfo.InvariantInfo), Now, FirstDayOfWeek.Monday, FirstWeekOfYear.Jan1)) >= My.Settings.Tijdverschil Then
bel.Add(nm)
End If
Next
I get the FormatException was unhandled at the if line.
In the error description it says (roughly translated to english):
The tokens aren't recognized at valid DateTime.
--edit--
If anyone thinks the format in excel is wrong, i copied one field over, they are all like this.
1408180736
Without additional information, I wonder if your date from Excel is coming in as an OLE Automation Date. Depending on how you read the data from Excel, it may come back in this format.
If it is, you need to parse it as a double and then as an OLEDate. Something like this:
Dim oleDate as Double
Dim result as DateTime
If Double.TryParse(excelWS.Cells(nm, 1).value(), oleDate) Then
' Handle valid OLE Automation dates...
result = DateTime.FromOADate(oleDate)
End If
The trick for this is include a datetime picker in your application set visibility to false.
Then set the string as value to the datetime picker and make use of the datetime picker to get the difference between dates.

dr.Item("Value1") has value but always return 0 when assigned to another variable

I need to modify some VB.net code. There is a strange problem that I am facing. I am retrieving value from a DataTable and trying to assign it to a variable. When I check the value of some column in QuickWatch window then it has value but when I assign it to a variable then 0 is returned to the variable. Below is the simple statement that is causing the problem.
Dim MyAmount As Double = Double.Parse(dr.Item("Amount").ToString)
In the QuickWatch window when I check dr.Item("Amount") then it has value 30.12 and after executing the above statement MyAmount has value 0. May be VB.net work somewhat different that I do not know?
Edit:
It is kind of wierd that above mentioned statement is not returning value. The following statement is running absolutely fine.
Dim tmpVar As String() = dr.Item("Amount").ToString.Split(".")
Latest Edit:
I think it has become more wierd. The problem does not seem to be related with dr.Item("Amount"). Suppose I want to store the current culture value in a variable by following code,
Dim CultureInformation As String = System.Globalization.CultureInfo.CurrentCulture.DisplayName
Now CutlureInformation variable after the statement is executed contains "nothing" but the DisplayName has value of English (United States). So I think the problem is somewhere else.
You should be using this syntax:
Dim MyAmount As Double = dr.Field(Of Double)("Amount")
I am not sure why you are getting this behavior - your line should work too.
You can also try this:
Dim MyAmount As Double = DirectCast(dr.Item("Amount"), Double)
When facing a weird issue like this, always try various options to achieve the same result, do your research and compare the outputs. It greatly helps to answer a question on StackOverflow.