Access. Alternative for left function - sql

i am trying to write a query using left function in access to take only the first 3 characters of a field.
Is there any alternative method for performing the same process without using left
Usage of left function shows a compile. error all of a sudden without any reason. If i copy the table and query to a new database it works fine for a while before the error comes again. This happens only on the usage of Left function.

you can use mid function:
Mid (Field, 1, 3)

The compile error is showing up because you have a missing reference. Open any module and check the References.

You can always try Mid
Mid([field1],1,3)

It sounds very like you have a problem with your references. Look for any references marked "MISSING". Also try to delete Visual Basic for Applications, it won't allow this, but it sometimes corrects the problem. Finally, check the details of Visual Basic for Applications and make sure that is available in the stated location. Any alternative to Left will be affected by this problem.
This problem is frequently associated with a missing reference that you would not think had anything to do with Left.

Run the following code and report back as to the results. Also tell us what version of Access you are running.
Sub ViewReferenceDetails()
Dim ref As Reference
For Each ref In Access.References
Debug.Print ref.Name & " - " & ref.Major & "." & ref.Minor & " - " & ref.FullPath
Next ref
End Sub

Related

MS Access: "Cannot open any more databases."

The Access error, "Cannot open any more databases.", has been discussed several times on StackOverflow[1, 2, 3, 4]. There's also an interesting discussion of the error on Bytes.com, in which Allen Brown and David Fenton weigh in on what can cause the error and argue about whether the limit behind it is about connections or table handles. None of the causes or solutions in those places apply to my situation, as far as I can tell.
I've got a complex database that was working well, but then started throwing this error. I've boiled down the VBA code and data structure to the following bare bones:
Option Explicit
Public Function TestFunction(ID_test_1 As Integer, nID_test_2 As Integer) As Variant
Dim oCurDb_Ftn As DAO.Database, oTestData As DAO.Recordset, sSQL As String, vTestCode As Variant
Set oCurDb_Ftn = CurrentDb()
sSQL = "SELECT * FROM t_test_2 WHERE ID_test_2 = " & nID_test_2 & ";"
Set oTestData = oCurDb_Ftn.OpenRecordset(sSQL)
vTestCode = oTestData![bValue]
oTestData.Close
Set oTestData = Nothing
Set oCurDb_Ftn = Nothing
TestFunction = vTestCode
End Function ' TestFunction
The numbers in the tables are arbitrary. The fields ID_test_n are primary keys. The code above is the entire contents of TestModule. The query TestQuery is:
SELECT t_test_1.ID_test_1, TestFunction(ID_test_1,3) AS [Test code]
FROM t_test_1;
When the query is opened, it at first appears to be okay. But if I scroll down through it, it throws the "cannot open any more" error:
I've discovered one way that I can get rid of the error. If I remove the first parameter of the function TestFunction(), so that it's definition becomes:
Public Function TestFunction(nID_test_2 As Integer) As Variant
...
and the call to it in the query becomes just:
TestFunction(3)
then I can scroll down and up through the query sheet multiple times without error. Those changes are possible because in the bare-bones code of the function, there is no reference to ID_test_1. But in the actual database, that parameter is passed for a reason and omitting it is not an option. Still, it is mysterious to me that whatever is causing the error does not happen if that parameter is not passed.
Can anyone see what's going on here, why I'm getting that error, and how to fix it without excluding parameters from the function?
Environment: Windows 10 Pro 64-bit, Access 2019.
This may be due to a bug in an Office update, about Jan 26, 2022. See Reddit post
Try system restore to roll back the update; or...
Go into Access, File Options, Trust center and add the local front end directory and then check the box to then add the backend data location as a trusted location.

For-Next loop not stopping

This seems crazy basic to ask, but I can't figure out what I've done wrong. Working in Access VBA to loop through an array, but the FOR loop doesn't stop and I get a "Subscript out of range" error.
compStr = "[" & uniqueIDs(0) & "]=" & rs(uniqueIDs(0))
bnd = UBound(uniqueIDs)
For i = 1 To bnd
compStr = compStr & " and [" & uniqueIDs(i) & "]=" & rs(uniqueIDs(i))
Next i
In this example I'm using the loop to build compStr and bnd stores the array size. uniqueIDs holds 2 records (0-1) and I've confirmed that bnd and UBound(uniqueIDs) value=1. However the FOR statement continues until i=2 and of course I get an error when it executes uniqueIDs(2) on the 4th line. What am I missing? (I've pasted debug window images below)
I did a test but that successfully ran, see below implementation:
Regarding above comments on the data types: I understand why people use Variants in Access: this is because often table values are assigned to variables, and only Variant can pick up NULL. However, in your case something forced the string values to be Strings and not Variants - if I used the line commented out, it would yield Variant/Variant(0 to 1), so I used a typed dummy array - I don't know how otherwise get Variant/String(0 to 1).
Nevertheless, there was no error. Did you compile your code with Debug -> Compile? If not, could you do it and re-try? Sometimes this is necessary, otherwise some code segments newly added/removed are just not executing and the previous compiled version of the code (which is stored by Access) is making a total mess.
If it's still not working, I think you should add the rest of the code as others asked in comments to see how the variables are assigned values, and the version of Access you are using.

Visio: DOS Sharing violation (Error 1532)

So I'm really confused right now. Out of the blue my code gets me the error "DOS Sharing violation".
It's weird because, it says that is trying to save my document, but I just want to open it.
This is my Code:
Public Sub ReadActivity()
Dim vsoDocument As Visio.Document
Dim vsoPage As Visio.Page
Set vsoDocument = Documents.Open("C:\Users\Philip\Dropbox\Test\Aktivität0.vsdx")
Set vsoPage = vsoDocument.Pages(1)
SvgExport (ActiveDocument.path & "\files_and_images\" & Left(ActiveDocument.name, (InStrRev(ActiveDocument.name, ".", -1, vbTextCompare) - 1)) & ".svg")
CreateCodeActivity
vsoDocument.Close
End Sub
So as you might see the code is simple nothing special is going on.
Before calling the method I'm using this for encoding my textfile: VBA : save a file with UTF-8 without BOM
And two things are very weird. First of all, I used this method two days in a row for coding the method "CreateCodeActivity" and I didn't have any problems. And second, if I call the method let's say three times, on the third time everything works perfectly...
Where might be the problem?
Thank you #Shmukko for the tip, it is really the windows defender that gives me the error.
For Windows 10 the solution is: Go to Settings and select Update & security -> Windows Defender. Select Exclude a file extension and enter the file type for Visio.
That's it.

Insert value from one table to another by pressing a button

I have a select query called "tbl_Update" and a table called "tbl_A" in Access DB. I would like to press on on a button, which makes the following:
looks in fields "KW" and when they match (in this example below, the value "2016.45" are matching), then insert the value of "tbl_update.CA041073p" into "tbl_A.CA041073p". Seems to be very easy, but getting always Errors to do it. Any simple solutions how to do it? Thanks!
My code is the following and it is giving me the Error 3037:
Public Function Update()
DoCmd.RunSQL "Update tbl_A INNER JOIN tbl_Update " & _
"ON tbl_A.KW = tbl_Update.KW " & _
"SET tbl_A.CA041073p = [tbl_Update].[CA041073p] "
End Function
One quite likely reason is that the user running the program doesn't have read-write access to the database file, especially if it is located in program files folder.
So check the directory and file permissions and modify them if needed. You can also consider changing the location of the database file to another, more easily accessible folder.

SSIS Element cannot be found in a collection (but I have them all listed!)

I'm getting a persistent error:
The element cannot be found in a collection.
This error happens when you try to retrieve an element from a collection on a container during execution of the package and the element is not there.
I've checked, double and triple-checked my variable listings in the Read-Only and Read-Write variables in my Script task.
I've debugged it to death and gotten input from another programmer here who couldn't spot the issue either.
I've also researched to no end.
Does anyone see anything wrong with my code?
Script Task code:
Public Sub Main()
Dts.Variables("User::strMailBody").Value = "Thank you for submission. For your convenience, we are including the last four of the HICN# and the Name on the application(s) we have received* from you." _
& vbNewLine & vbNewLine & "Here are the following: " & vbNewLine & vbNewLine
Dts.Variables("User::strMailBody").Value = Dts.Variables("User::strMailbody").Value.ToString() & vbNewLine & Dts.Variables("User::strListing").Value.ToString()
Dts.Variables("User::strMailBody").Value = Dts.Variables("User::strMailBody").Value.ToString() & vbNewLine & vbNewLine & Dts.Variables("User::strFooter").Value.ToString()
If Left(Dts.Variables("User::strAgentID").Value, 2) = "TX" Then
Dts.Variables("User::strSubject").Value = "ACME Health Plans Confirmation: Total "
Else
Dts.Variables("User::strSubject").Value = "ACME2 Baptist Health Plans Confirmation: Total "
End If
Dts.Variables("User::strSubject").Value = Dts.Variables("User::strSubject").Value.ToString() & Dts.Variables("User::lngCountAgent").Value.ToString() & " " & "[RESTRICTED: CONFIDENTIAL]"
Dts.Variables("User::DateSent").Value = Now()
Dts.Variables("User::UserSent").Value = "SSIS"
Dts.TaskResult = ScriptResults.Success
End Sub
For anybody else struggling with this issue the resolution for me was as follows: (note I am NOT using User:: when getting variable values within my script task)
On the package Properties I hadn't included the variables as ReadOnlyVariables
You'll need to set your newly added variables as follows:
Right click on the package and select Edit
In the Script section click on ReadOnlyVariables or ReadWriteVariables (depending on your how you want your variables behave)
Check the check-box beside the variables you wish to use in your script task
Click Ok to save your changes
Hope this helps
I just had the same issue and unable to find the problem for ages. I found that the reason for the error was that I had missed one of the colons between "User" and the variable name.
I had this (which caused the error):
string FileName = UserVariables["User:CurrentFileName"].Value.ToString();
when I should have had this:
string FileName = UserVariables["User::CurrentFileName"].Value.ToString();
just in case anyone else has the same problem :)
Ohhh.........man. It's amazing how you can stare at this stuff and miss something stupid, for hours.
strFooter was missing in the listing.
ALL SET NOW. Sincere thanks to those who looked and commented. Eric, thanks, I'll remember that as sometimes I will probably need to use C insatead of VB (haven't yet but will).
Had a similar issue, after a lot of debugging, realized that the variable naming convention should be User::varname and NOT USER::varname
I guess c# is very case sensitive.
Hope this helps and saves you lot of your valuable time :-)
a) Right click on the script task and choose edit
b) Locate the Read or Read/Write variables properties in the list.
c) Click on the property and the variable you wish to access in the script task.
Another variation on "have been staring at the screen for too long to see the typo". In my case, I got the same error by mixing up the syntax between Project Params and User variables, and adding a $ sign in front of User.
error :
string varA = (string)Dts.Variables["$Project::ParamA"].Value
string varB = (string)Dts.Variables["$User::ParamB"].Value
corrected :
string varA = (string)Dts.Variables["$Project::ParamA"].Value
string varB = (string)Dts.Variables["User::ParamB"].Value