(Error 3071) Access - ms-access

I'm trying to search for the Course ID by checking the the faculty ID and Course ID in my Course table. Its a simple query but when launched I receive a
(This expression is typed incorrectly, or it is too complex to be evaluated. For example, a numeric expression may contain too many complicated elements. Try simplifying the expression by assigning parts of the expression to variables. (Error 3071)
The VB script I'm using looks like this.
Private Sub Course_ID_DblClick(Cancel As Integer)
Dim x As Integer
Dim y As String
Dim z As Integer
x = Me.Faculty_ID.Value
Me.Course_ID.RowSource = "SELECT Course.[Course ID] FROM Course WHERE Course.[Course Name]=['Course']AND Course.[Faculty ID]='" & x & "'"
End Sub

I think you are getting this message because you are using wrong delimiters and confusing terminology in object names (where field content 'course' and table name [course] are the same.
I guess your datasource is an ms-access database. You should then use the " (double quote) as value separator (you'll have to double the sign inside the expression) for text, and nothing for numbers. your select instruction could then look like:
"SELECT Course.[Course ID] FROM Course WHERE Course.[CourseName]=[""Course""] AND Course[FacultyID]=" & x
Add a 'debug.print Me.Course_ID.RowSource' to check the final string that should look like:
SELECT Course.[Course ID] FROM Course WHERE Course.[CourseName]=["Course"] AND Course[FacultyID]= 3

There should be no brackets around your string literals:
Me.Course_ID.RowSource = "SELECT Course.[Course ID] FROM Course WHERE Course.[Course Name]='xyz' AND ...
Also, you can use either single or double quotes as string delimiters in an access query.

Related

field with memo datatype in a recordset

Is there any specific number of characters in string while use it in where clause?
SQL2 = "SELECT DISTINCT " & a & " AS MyString," & b & " FROM tempExtractedtbl"
Set rsGenerate = CurrentDb().OpenRecordset(SQL2)
Do While Not rsGenerate.EOF
SQL3 = "Select intId from IPDMst where TXTliteral='" &
rsGenerate.Fields("MyString").Value & "'"
Set temprsGenerate = CurrentDb().OpenRecordset(SQL3)
Here value of MyString in tempExtractedtbl is:
PO_EDGE_00503|Device Charge_1|Device
Type~Smartphone~Manufacturer~Apple~Model~iPhone 6~Capacity~32
GB~Colour~Space Grey~Blue Tick~FALSE~4GX~TRUE~Splash Proof~FALSE~Removable
Battery~FALSE~Expandable Memory~FALSE~HD Video
Recording~TRUE~NFC~TRUE~Featured~FALSE~Bonus Gift~FALSE~Operating
System~ios~Hot Buy~FALSE~Root Offering~Mobility Offer~
But when its stored in recordset, the value of MyString is trimmed to 255 characters. When i execute
?rsGenerate.Fields("MyString").Value
in immediate window, I get MyString as
PO_EDGE_00503|Device Charge_1|Device
Type~Smartphone~Manufacturer~Apple~Model~iPhone 6~Capacity~32
GB~Colour~Space Grey~Blue Tick~FALSE~4GX~TRUE~Splash Proof~FALSE~Removable
Battery~FALSE~Expandable Memory~FALSE~HD Video
Recording~TRUE~NFC~TRUE~Featured~F
And when I execute SQL3 I get following errors as MyString is trimmed.
Is there any limitation of recordset for memo field as MyString is a field having memo datatype. Is there a way out to store string having length more than 255 characters in a recordset and use it?
DISTINCT and Memo fields don't go together.
http://allenbrowne.com/ser-63.html
Access truncates the memo if you ask it to process the data based on the memo: aggregating, de-duplicating, formatting, and so on.
Uniqueness: Since you asked the query to return only distinct values, Access must compare the memo field against all other records. The comparison causes truncation.
Remove DISTINCT from the SELECT SQL.
You may need to create another query that selects the distinct values without the memo, and then use it as the source for another query that retrieves the memo without de-duplicating.

Pass in Delimited String to Access Query as Parameter

I am trying to pass in a string to an Access query that has a parameter "companyType" . The sql is "where companyType in ([forms]![formname].[fieldname])"
This works fine with one value but for string e.g "CompanyType1","CompanyType2" it does not work.
I know the in operator needs to have each element in quotes if the data type for that field is a Short Text or Long text type. I have tried wrapping them in single quotes also to no avail.
When I hard code the values in the query e.g "in ('CompanyType1','CompanyType2')" query returns rows so I believe it is something with escaping the quotes but not exactly sure.
You won't be able to put this into an access query as a parameter; However you could build the query in vba:
dim qry as new QueryDef
qry.SQL = _
"SELECT some_column, ... " & _
"FROM some_table [INNER JOIN ...] " & _
"WHERE CompanyType IN (" & [list] & ")"
qry.Execute

Characters appended to a string all appear as question marks in VBA

I have been using the same code to get the fullname from the current user for a while now and it suddenly started behaving weird.
I have a string variable named CurrentUser which I populate with my function GetFullNameOfLoggedUser().
This return in the immediate window (msgbox also works fine) "Lastname, Firstname (F)" which it is supposed to.
When I want to give the variable to another string variable called 'sql1', it starts to behave weird all of a sudden:
CurrentUser = GetFullNameOfLoggedUser()
Dim sql1 As String: sql1 = "UPDATE Tbl_Records SET Tbl_Records.[Added by] = """ & CurrentUser & """ WHERE Tbl_Records.[Added by] IS NULL;"
The value of sql1 suddenly becomes:
UPDATE Tbl_Records SET Tbl_Records.[Added by] = "Lastname, Firstname (F)? ????? ?????????????????? ??? ?? ?????
Does anybody have a clue where all the question marks come from?
Disclaimer Lastname and Firstname are obviously regular values, they are omitted for the sake of privacy.
Extra info:
To get the full network name, I am using this fine piece of code from Dev Ashish which makes use of windows API:
http://access.mvps.org/access/api/api0066.htm
The function has been dimmed as string. I have added an "" at the end of the function to ensure the value is a string type:
Function GetFullNameOfLoggedUser(Optional strUserName As String) As String
...
GetFullNameOfLoggedUser = StrFromPtrW(pTmp.EUI_full_name) & ""
End Function
As seen in the locals window, it truly is a string. (This snap has been taken right before the end of the funcion, so no further changes will happen to the variable.
CurrentUser has also explicitly been defined as a string variable. Option Explicit is also active on every page.
There does seem to be something a bit strange about the string that fGetFullNameOfLoggedUser() returns. When I run the code
Dim s As String
s = fGetFullNameOfLoggedUser()
Debug.Print Len(s)
Debug.Print s
I get
13
GORD THOMPSON
which looks correct. However, if I change the last Debug.Print to
Debug.Print "|" & s & "|"
I get
|GORD THOMPSON?
with the final character being a question mark ? instead of a pipe |. However, if I Trim() the string
Debug.Print "|" & Trim(s) & "|"
then I get
|GORD THOMPSON|

How to include parameter in sql command in access VBA?

I am new to Access VBA. I am passing a parameter (t) which is the result of a function(myfunc) to a table(myTable), it has ID and time column. Here is my code. It does not work. Where is the problem? Thanks.
Sub test()
Dim timetem As String
Dim t As String * 50
timetem = DLookup("[time]", "myTable", "[ID]=1")
t = myfunc(timetem)
DoCmd.RunSQL "UPDATE myTable SET [time] ='" & t & "'WHERE [ID] =1;"
End Sub
Please post the specific error code you're getting.
There are two issues with your code,
Firstly, what datatype is [time]?
If the time column in the database is not a string, this code will not work.
You should match the data type of timetem with the data type of the [time] column in your DB.
If time should be a date/time field then t should be escaped with # instead of '
I think there should also be a space here:
"'WHERE [ID]
should probably be
"' WHERE [ID]
Other pointers: You don't need to specify the size of t. Actually if you're trying to pass a date type as a string you probably would need to use the Format() function.
Also, DLookup is evil.

Adding logic to a sub form in Access?

I have a sub form in Access:
The CopyNo is a combobox that lets me select from the MovieCopies table. When I select one, I want the Title field to show the correct movie title associated to that copy's movie ID. I also want the format to show. When I select from DaysRented combobox, if I select 1 and the movie is New, I want it to display the price, if it is regular for 3 days display the correct price etc.
I'm just not sure how to give logic to the comboboxes.
If anyone could point me in the right direction of how to do this sort of thing in Access 2007 I'd really appreciate it.
Thanks
Something like this:
Private Sub cboCopyNo_AfterUpdate()
If Nz(Me.cboCopyNo, "") <> "" Then
Me.txtTitle = DLookup("Title", "MovieMaster", "MovieID = " & Me.cboCopyNo)
End If
End Sub
Private Sub cboDaysRented_AfterUpdate()
If Nz(Me.cboDaysRented, 0) > 0 Then
Dim strType as String
strType = DLookup("[Type]", "MovieMaster", "MovieID = " & Me.cboCopyNo)
If Me.cboDaysRented = 1 Then
Me.txtPrice = DLookup("Price1Day", "Price", "[Type] = '" & strType & "'")
Else
Me.txtPrice = DLookup("Price3Day", "Price", "[Type] = '" & strType & "'")
End If
End If
End Sub
Couple notes. Some of your field names are reserved words in certain databases, such as "Type". I highly recommend you try to use field names that are not reserved words in Access or SQL server.
DLookups are not necessarily the fastest way to lookup data but will likely be fast enough for what you're trying to do here. Sometimes I create my own DAO recordset and lookup the values I want rather than using DLookup. It's basically like writing your own DLookup function.
DLookup uses SQL language so your syntax in the third argument, the WHERE clause, needs to match SQL. If the field in your WHERE clause is text/string you'll need to use a single quote on either side of the value (as shown above around the strType variable). If it is a number field you will not need the quotes. If it's a date you'll need hash signs (#).