Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Problem with retrieve and display data with periods, commas and apostrophes in the data

Posted on 2008-10-06
9
Medium Priority
?
394 Views
Last Modified: 2012-06-27
I have data that has things like "St. John's" that were entered and then saved to access database.  When I use access query the data displays fine, however when I use the code I sent to populate a combobox, the period triggers a shift in the data so the combobox that has 2 columns would display column 1 "St" then column 2 would have "John" then column 1 next row would have "s" and then column 2 2nd row would have "141" which is a record ID.  If I were to put a ms access query in the recordsource it works fine but when I write a sql statement to populate the box I get these weird displays.  I can't use an access query as the record source as it needs to change.  Please help fast!
'This populates the combo with the table data and the NEW item.
    Me.CbxCmpnyTypNm.RowSource = ""
    
    'This add the New Item
    Me.CbxCmpnyTypNm.AddItem ("0;<New Record>")
    
    'This sets some initial values to work with
    Dim db As Object
    Dim rcrdst As Object
    Dim strCmpnyID As String
    Set db = CurrentDb
    
    'This tells the system if you get an error go there.
    On Error GoTo Error_Handler
    
    'This gets the records from the database.
    Set rcrdst = db.OpenRecordset("SELECT tblCmpnyTyp.CmpnyTypID, tblCmpnyTyp.CmpnyTypNm, tblCmpnyTyp.StatID, tblStat.StatNm " & _
    "FROM tblStat INNER JOIN tblCmpnyTyp ON tblStat.StatID = tblCmpnyTyp.StatID " & _
    "WHERE tblCmpnyTyp.StatID = 1 " & _
    "ORDER BY tblCmpnyTyp.CmpnyTypNm")
    
    'This loops thru to add items from the table
    rcrdst.MoveFirst
        Do Until rcrdst.EOF
            Me.CbxCmpnyTypNm.AddItem (rcrdst.Fields("CmpnyTypID") & ";" & rcrdst.Fields("CmpnyTypNm"))
            rcrdst.MoveNext
        Loop
    rcrdst.Close
    Exit Sub

Open in new window

0
Comment
Question by:avalonwgi
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
9 Comments
 
LVL 28

Expert Comment

by:TextReport
ID: 22650178
For the apostrophe then use the replace function to replace a single apostrophe with 2 apostrophes

Me.CbxCmpnyTypNm.AddItem (rcrdst.Fields("CmpnyTypID") & ";" & Replace(rcrdst.Fields("CmpnyTypNm"),Chr(39),Chr(39) & Chr(39))

I use Chr(39) for the apostrophe as it is easier to read than the use of ' and '' and " etc

As for the full stop then I am not sure but you can try replacing it with 2 full stops and replace it with an empty string

Me.CbxCmpnyTypNm.AddItem (rcrdst.Fields("CmpnyTypID") & ";" & Replace(Replace(rcrdst.Fields("CmpnyTypNm"),Chr(39),Chr(39) & Chr(39),".",".."))

or

Me.CbxCmpnyTypNm.AddItem (rcrdst.Fields("CmpnyTypID") & ";" & Replace(Replace(rcrdst.Fields("CmpnyTypNm"),Chr(39),Chr(39) & Chr(39),".",""))

Cheers, Andrew
0
 

Author Comment

by:avalonwgi
ID: 22650331
Andrew, I have used the replace function in the past on others to address the apostrophe issue, however that does not fix the unplanned breaks in the data retrieval I am experiencing.  And, I don't understand your last two examples of the replace.  I don't know nor have never used the Chr(39) thing.  I am still a beginners so any easier way to explain would be appreciated.  The main problem is that when I load the combobox with the code, once it sees a period in St. Johns or Dave, INC.  it thinks that is the end of the data / column and moves it to the next data point or next row in the combobox
0
 
LVL 28

Expert Comment

by:TextReport
ID: 22650459
OK the CHR function returns a character, 39 being the apostrophe so using Chr(39) is the same as "'" (this is double quote, apostrophe and another double quote) whis is very difficult to read.

The last 2 examples use multiple / nested replaces
This one replace a single apostrophe with 2 apostrophes then replaces the full stop with 2 full stops.
Me.CbxCmpnyTypNm.AddItem (rcrdst.Fields("CmpnyTypID") & ";" & Replace(Replace(rcrdst.Fields("CmpnyTypNm"),Chr(39),Chr(39) & Chr(39),".",".."))

This one replace a single apostrophe with 2 apostrophes then replaces the full stop with nothing.
Me.CbxCmpnyTypNm.AddItem (rcrdst.Fields("CmpnyTypID") & ";" & Replace(Replace(rcrdst.Fields("CmpnyTypNm"),Chr(39),Chr(39) & Chr(39),".",""))

Finally it may be you need to put double quotes around the text field
Me.CbxCmpnyTypNm.AddItem (rcrdst.Fields("CmpnyTypID") & ";" & Chr(34) & rcrdst.Fields("CmpnyTypNm") & Chr(34))

Yes Chr(34) is the same as """" 4 double quotes.

Hope this makes sense.
Andrew
0
Learn Veeam advantages over legacy backup

Every day, more and more legacy backup customers switch to Veeam. Technologies designed for the client-server era cannot restore any IT service running in the hybrid cloud within seconds. Learn top Veeam advantages over legacy backup and get Veeam for the price of your renewal

 
LVL 75
ID: 22652026
"Yes Chr(34) is the same as """" 4 double quotes."

Actually, Chr(34) is one double quote  "

mx
0
 
LVL 75
ID: 22652072
In general in Access SQL, you should avoid using any Single Quotes in the SQL string - for exactly the problem you are having.  Only double quotes should be used.   If single quotes are used, then any string element that contains a single quote or other special characters becomes problematic, as you can see.

Also - a side note - the InStr() function will not present an issue with strings that contain special characters - if you are trying to determine if a special character is present in a string.

mx
0
 
LVL 28

Expert Comment

by:TextReport
ID: 22652598
DatabaseMX, "Actually, Chr(34) is one double quote " yes it is one double quote, the context of it being the same as 4 double quotes is that then you are doing string manipulation you are qiven a choice of

strName = "MX"
strMSG = "Andrew said ""Hello" &  strName & """"
or
strMSG = "Andrew said ""Hello" & strName & Chr(34)

Both of the examples above with have the value of strMSG or
Andrew said "Hello MX"

Cheers, Andrew
0
 

Author Comment

by:avalonwgi
ID: 22653706
Andrew and MX,
I am still not sure I understand what to do.  I have data in company name field like Davlen, Inc. or St. John's Coal, etc.  They are stored and displayed in access tables correctly.  They also display correctly using the query in access.  However, they do not display correctly when populating a combobox that has company ID and company name.  I have enclosed a screen shot of what I am seeing.

SO, what do I need to do to the code that I have attached to get this to work?
Private Sub FillCbxCmpnyNm()
    'This populates the combo with the table data and the NEW item.
    Me.CbxCmpnyNm.RowSource = ""
    
    'This add the New Item
    Me.CbxCmpnyNm.AddItem ("0;<New Record>")
    
    'This sets some initial values to work with
    Dim db As Object
    Dim rcrdst As Object
    Dim strCmpnyID As String
    Set db = CurrentDb
    
    'This tells the system if you get an error go there.
    On Error GoTo Error_Handler
    
    'This gets the records from the database.
    Set rcrdst = db.OpenRecordset("SELECT tblCmpny.CmpnyID, tblCmpny.CmpnyNm, tblCmpny.StatID, tblStat.StatNm " & _
    "FROM tblStat INNER JOIN tblCmpny ON tblStat.StatID = tblCmpny.StatID " & _
    "WHERE tblCmpny.StatID = 1 " & _
    "ORDER BY tblCmpny.CmpnyNm")
    
    'This loops thru to add items from the table
    rcrdst.MoveFirst
        Do Until rcrdst.EOF
            Me.CbxCmpnyNm.AddItem (rcrdst.Fields("CmpnyID") & ";" & rcrdst.Fields("CmpnyNm"))
            rcrdst.MoveNext
        Loop
    rcrdst.Close
    Exit Sub
    
    'This is if there was an error it would jump to.
Error_Handler:
    MsgBox "Fill Combo Error: " & Err.Description
End Sub

Open in new window

Doc1.doc
0
 
LVL 28

Accepted Solution

by:
TextReport earned 1500 total points
ID: 22654591
I have tested this in Access 2002 without the problems you are encountering. Please ensure the Row Source Type is a Values List and that your column count is set to 2.

Please confirm the version of Access you are using and can you not use your query as the RowSource rather than using code.

Cheers, Andrew
0
 

Author Closing Comment

by:avalonwgi
ID: 31503418
Perhaps I didn't word the question correctly.
0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
Traditionally, the method to display pictures in Access forms and reports is to first download them from URLs to a folder, record the path in a table and then let the form or report pull the pictures from that folder. But why not let Windows retr…
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

704 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