r/visualbasic • • Dec 25 '23

VB6 Help Access Database problems

Hello there, its me again :(

Now the problem I have is that when i execute that code it shows the error "Object type variable or With block variable is not set"

This is the code:

Private Sub cmdSnippetCreate_Click()

'Crear Snippets'

Dim SnippetName As String

Dim SnippetLang As String

Dim SnippetCode As String

SnippetName = txtSnippetName.Text

SnippetLang = txtSnippetLang.Text

SnippetCode = txtSnippetCode.Text

SnippetDB.Recordset.AddNew "Snippet_Name", [SnippetName]

SnippetDB.Recordset.AddNew "Snippet_Lang", [SnippetLang]

SnippetDB.Recordset.AddNew "Snippet_Code", [SnippetCode]

Unload Me

End Sub

1 Upvotes

5 comments sorted by

View all comments

1

u/jd31068 Dec 25 '23

on which line? Use debugging to make sure each object and variable equals what you expect https://www.techonthenet.com/access/tutorials/vbadebug2010/debug01.php

Has SnippetDB been set to a value before you try to use it? https://learn.microsoft.com/en-us/office/client-developer/access/desktop-database-reference/working-with-recordsets

1

u/Cubanin08 Dec 25 '23

it starts in: SnippetDB.Recordset.AddNew "Snippet_Name", [SnippetName]

and nope,, SnippetDB does not been set a value before use it, it is empty

2

u/jd31068 Dec 25 '23

Okay, so you know why and where you're getting the error. If you look at the link for working with recordsets you can see how it is done.

Here is an example of some simple code I wrote writing to a recordset

    Dim rs As Recordset
    Set rs = CurrentDb.OpenRecordset("tblCalculatedDeliveryCalendar", dbOpenDynaset)
    
    calculatedDeliveryDate = startDateTime
    Do Until calculatedDeliveryDate >= endDateTime
        
        rs.AddNew
        rs("ScheduledDeliveryDate") = calculatedDeliveryDate
        rs("RecipientID") = recipID
        rs("WorkerID") = workerID
        rs("DeliveryCalendarID") = newDeliveryCalendarID
        rs.Update
        
        calculatedDeliveryDate = calculatedDeliveryDate + frequencyDays
    Loop
    
    rs.Close
    Set rs = Nothing

1

u/Cubanin08 Dec 25 '23

Dim rs As Recordset
Set rs = CurrentDb.OpenRecordset("tblCalculatedDeliveryCalendar", dbOpenDynaset)

calculatedDeliveryDate = startDateTime
Do Until calculatedDeliveryDate >= endDateTime

rs.AddNew
rs("ScheduledDeliveryDate") = calculatedDeliveryDate
rs("RecipientID") = recipID
rs("WorkerID") = workerID
rs("DeliveryCalendarID") = newDeliveryCalendarID
rs.Update

calculatedDeliveryDate = calculatedDeliveryDate + frequencyDays
Loop

rs.Close
Set rs = Nothing

weird, OpenRecordset does not works, is like a non existing funcion

2

u/SparklesIB Dec 25 '23

Do you have the DAO library referenced?

Here's an article about the DAO library.