Solved ADODB In Memory Recordset (1 Viewer)

ADIGA88

Member
Local time
Today, 15:07
Joined
Apr 5, 2020
Messages
94
Hi guys,

I am trying to create an in-memory ADO recordset and bind it to a form, while I am able to create and bind the recordset to the form, the textbox will not bind with the recordset field.

Is this possible at all or I am doing something wrong?

Code:
Dim cn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim myForm As Form_Form1
Sub RunMe()

    Set cn = New ADODB.Connection
    cn.ConnectionString = CurrentProject.Connection
    cn.CursorLocation = adUseClient
    cn.Open
    Set rs = New ADODB.Recordset
    rs.Fields.Append "Name", adChar, 20
    rs.Open
    rs.AddNew
    rs.Fields(0) = "Entry 1"
    rs.Update
    rs.AddNew
    rs.Fields(0) = "Entry 2"
    rs.Update
    
    Set myForm = New Form_Form1
    myForm.Visible = True
    Set myForm.Recordset = rs
    
End Sub

Thanks
 

Attachments

  • Screenshot 2023-03-28 161757.png
    Screenshot 2023-03-28 161757.png
    33 KB · Views: 62
  • Screenshot 2023-03-28 161748.png
    Screenshot 2023-03-28 161748.png
    31.8 KB · Views: 61

MajP

You've got your good things, and you've got mine.
Local time
Today, 08:07
Joined
May 21, 2018
Messages
8,529
Since Name is a reserved word that could cause some problems. Rename the field to something else and try.
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 20:07
Joined
May 7, 2009
Messages
19,245
you don't need Connection there:
Code:
'Dim cn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim myForm As Form

Sub RunMe()

    'Set cn = New ADODB.Connection
    'cn.ConnectionString = CurrentProject.Connection
    'cn.CursorLocation = adUseClient
    'cn.Open
    Set rs = New ADODB.Recordset
    rs.CursorLocation = adUseClient
    rs.LockType = adLockOptimistic
    rs.Fields.Append "Name", adVarChar, 20
    rs.Open
    rs.AddNew
    rs.Fields(0) = "Entry 1"
    rs.Update
    rs.AddNew
    rs.Fields(0) = "Entry 2"
    rs.Update
    
    rs.MoveFirst
    
    Set myForm = New Form_Form1
    
    With myForm
        !Text0.ControlSource = "Name"
        Set .Recordset = rs
        .Visible = True
    End With
    
End Sub
 

ADIGA88

Member
Local time
Today, 15:07
Joined
Apr 5, 2020
Messages
94
Thank you guys it is working.

first, it wasn't the name I tried others but didn't work.

@arnelgp your code worked as it is, and really is no need for the cn even I forget to pass it to the rs.open. and after playing with your code I found out that the issue was lock type, I didn't have to reset the ControlSource for the textbox or set CursorLocation to be client side to make it work.

Again Thanks
 

ADIGA88

Member
Local time
Today, 15:07
Joined
Apr 5, 2020
Messages
94
a follow up question, why the textbox has a white space after it?

Thanks
 

Attachments

  • 1680079762531.png
    1680079762531.png
    26.9 KB · Views: 67
  • 1680079898532.png
    1680079898532.png
    48.9 KB · Views: 71

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 20:07
Joined
May 7, 2009
Messages
19,245
because adChar is "fixed" length string, use adVarChar (variable length) instead.
 

Users who are viewing this thread

Top Bottom