Solved

How to use Mid function in SQL statement?

Posted on 2008-06-10
5
1,087 Views
Last Modified: 2013-11-27
I have the following codes that look for the common records of two tables and join the caseIDs for these common records as one string:

With CurrentDb.OpenRecordset("SELECT tbl_A.* FROM tbl_A INNER JOIN tbl_B ON " _
& "Mid(tlb_A.aFieldThatContainsCaseID, 58, 5) = tbl_B.caseID WHERE tbl_A.fld_1 IS NOT NULL")
  While Not .EOF
  existVal = Mid(.Fields("aFieldThatContainsCaseID"), 58, 5) & " " & existVal
  .MoveNext
  Wend
.Close
End With

But error occurs saying type mismatch. I think the problem lies in the Mid function in the SQL statement. Can anyone suggest a way out?
0
Comment
Question by:angie_angie
  • 2
  • 2
5 Comments
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 21749841
in sql server, the "mid" function is calls substring.
0
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 85 total points
ID: 21749846
sorry, drop that ;)


With CurrentDb.OpenRecordset("SELECT tbl_A.* FROM tbl_A INNER JOIN tbl_B ON " _
& " ( Mid(tlb_A.aFieldThatContainsCaseID, 58, 5) = str(tbl_B.caseID) ) WHERE tbl_A.fld_1 IS NOT NULL")
  While Not .EOF
  existVal = Mid(.Fields("aFieldThatContainsCaseID"), 58, 5) & " " & existVal
  .MoveNext
  Wend
.Close
End With

Open in new window

0
 

Author Comment

by:angie_angie
ID: 21756156
angellll,

I tried your codes... Although no error shows, there is nothing written to the string existVal.

Can you help?
0
 
LVL 74

Assisted Solution

by:Jeffrey Coachman
Jeffrey Coachman earned 40 total points
ID: 21756634
angie_angie,

First, ...Is the actual field name: "aFieldThatContainsCaseID"  ???
If not, then that might be your problem.

Second, I would check to see, what, if anything is being returned by the Mid() function.

So I would make a simple form from the table, with only the "aFieldThatContainsCaseID" Field.
I would drop a second textbox on the form, and make the controlsource:
=Mid([aFieldThatContainsCaseID], 58, 5)

Now just cycle through the records, and make sure that the correct value is displayed in the second textbox (For all the records).

JeffCoachman
0
 

Author Comment

by:angie_angie
ID: 21757395
The problem is solved by using Val to convert whatever Mid function returns to numbers.

Thank you guys.
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

'Between' is such a common word we rarely think about it but in SQL it has a very specific definition we should be aware of. While most database vendors will have their own unique phrases to describe it (see references at end) the concept in common …
I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question