Importing data from Access table into Excel Userform Textbox

mr_nic

New Member
Joined
Aug 14, 2012
Messages
4
Hi all,

I'm trying to extract data from an Access DB into an Excel user form (as the intended users will not all have MS Access).
The access table (TblData) has multiple rows and fields, and the SQL query I have created will search the 'CaseRef' field for the value the user puts in to the first text box, and then return the value from 2 specific fields into the two text boxes below.

The issue I am having is that the values that are being returned have the character "¶" added to the end.
Furthermore, if I try to copy the value, it pastes as "??" or two square boxes.

Would someone be able to tell me where I'm going wrong, or what I should change?

The code is below - many thanks for your help!

Code:
Private Sub CommandButton1_Click()
    Dim Cn As ADODB.Connection
    Set Cn = New ADODB.Connection
    Dim Rs As ADODB.Recordset
    Set Rs = New ADODB.Recordset
    Dim strDataSQL As String
    strDataSQL = "SELECT divsion FROM TblData WHERE CaseRef=" & Nums
    Dim Db As String
    Db = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=c:\users\user\documents\kyc database.accdb;Persist Security Info=False;"
    Dim Nums As String
    Nums = Me.CaseNum.Value 'this is the reference the user inputs'
     
    If CaseNum = 0 Then
        MsgBox "Please enter a value"
        Exit Sub
        End If
    
    Cn.Open Db
    With Rs
        .ActiveConnection = Cn
        .Open "SELECT division FROM TblData WHERE CaseRef=" & Nums, _
        Cn, adOpenStatic
    TestForm.DivBox.Value = Rs.GetString
    End With
    Rs.Close
    
    With Rs
        .ActiveConnection = Cn
        .Open "SELECT casehandler FROM TblData WHERE CaseRef=" & Nums, _
        Cn, adOpenStatic
    TestForm.WorkerBox.Value = Rs.GetString
    Rs.Close
    Set Rs = Nothing
    Cn.Close
    Set Cn = Nothing
    End With
    
End Sub
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
TestForm.DivBox.Value = Rs.GetString

rs,GetString is unusual.

What you want is:
Code:
TestForm.WorkerBox.Value = Rs.Fields(0).Value
Or
Code:
TestForm.WorkerBox.Value = Rs.Fields("division").Value

You can do this in one query if they are the same CaseRef record:
Code:
    With Rs
        .ActiveConnection = Cn
        .Open "SELECT [COLOR="#FF0000"]division, casehandler[/COLOR] FROM TblData WHERE CaseRef=" & Nums, _
        Cn, adOpenStatic
    TestForm.DivBox.Value = Rs.Fields("division").Value
    TestForm.WorkerBox.Value = Rs.Fields("casehandler").Value
    End With
    Rs.Close
 
Upvote 0
Hi Xenou,

thanks so much for your help - this has worked perfectly.
I've got multiple entries to display on the userform, so I've taken the combined approach suggested last.

I'm going to close this thread as you've solved my query, but I have another question with regards to using the 'INSERT INTO' sql function - if you spy it in the forum and are able to help again i'd be very grateful!!

Mr Nic
 
Upvote 0

Forum statistics

Threads
1,214,653
Messages
6,120,750
Members
448,989
Latest member
mariah3

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top