VBA UserForm connected to Maria DB

Shadowshiroy

New Member
Joined
Jul 18, 2023
Messages
3
Office Version
  1. 365
Platform
  1. Windows
Good Morning,

I have been working on a VBA code in order to modify data in a MariaDB database through a UserForm in Excel. The first time I run it works perfectly, the problem arises when I save it and try to open it again. It gives me a message of corrupted file and erases the VBA code. I think the problem is allocated in the Module code, when I define the macro to show the UserForm through a button displayed in Excel as just saving the UserForm does not arise any problem.


I attach the codes I have been using:

VBA Code:
**MODULE CODE**
Sub Enterdata()
UserForm1.Show
End Sub

**USERFORM CODE**
*ADD DATA*
Private Sub CommandButton1_Click()
    
    Dim valor As String
    Dim rowAffected As Integer
    
    valor = TextBox1.Value
    
    Dim con As ADODB.Connection
    Set con = New ADODB.Connection
    con.Open "DRIVER={MariaDB ODBC 3.1 Driver};SERVER=XX;PORT=XX;DATABASE=XX;UID=XX;PWD=XX;"
    
    Sql = "INSERT INTO tipusprojecte(TipusProjecte) values('" & valor & "');"
    con.Execute Sql, rowAffected

    If rowAffected = 1 Then
        MsgBox "Añadido"
    Else
        MsgBox "Fail"
    End If
    
    con.Close
    
End Sub

*DELETE DATA*
Private Sub CommandButton2_Click()
    
    Dim valor As String
    Dim rowAffected As Integer
    
    valor = TextBox1.Value
    
    Dim con As ADODB.Connection
    Set con = New ADODB.Connection
    con.Open "DRIVER={MariaDB ODBC 3.1 Driver};SERVER=XX;PORT=XX;DATABASE=XX;UID=XX;PWD=XX;"
    
    Sql = "DELETE FROM tipusprojecte WHERE TipusProjecte = '" & valor & "';"
    con.Execute Sql, rowAffected

    If rowAffected = 1 Then
        MsgBox "Eliminado"
    Else
        MsgBox "Fail"
    End If
    
    con.Close
    
End Sub

*UPDATE DATA*
Private Sub CommandButton3_Click()
    
    Dim valor As String
    Dim rowAffected As Integer
    
    valor = TextBox1.Value
    valorini = TextBox2.Value
    
    Dim con As ADODB.Connection
    Set con = New ADODB.Connection
    con.Open "DRIVER={MariaDB ODBC 3.1 Driver};SERVER=XX;PORT=XX;DATABASE=XX;UID=XX;PWD=XX;"
    
    Sql = "UPDATE tipusprojecte SET TipusProjecte = '" & valor & "' WHERE TipusProjecte = '" & valorini & "';"
    con.Execute Sql, rowAffected

    If rowAffected = 1 Then
        MsgBox "Actualizado"
    Else
        MsgBox "Fail"
    End If
    
    con.Close
    
End Sub
*RESET ID*
Private Sub CommandButton4_Click()

    Dim valor As String
    Dim id As Integer
    Dim rowAffected As Integer
    
    valor = TextBox1.Value
    valorini = TextBox2.Value
    id = CInt(TextBox3.Value)
    
    Dim con As ADODB.Connection
    Set con = New ADODB.Connection
    con.Open "DRIVER={MariaDB ODBC 3.1 Driver};SERVER=XX;PORT=XX;DATABASE=XX;UID=XX;PWD=XX;"
    Sql = "ALTER TABLE tipusprojecte AUTO_INCREMENT = " & id & ";"
    con.Execute Sql, rowAffected

    If rowAffected = 1 Then
        MsgBox "Reset"
    Else
        MsgBox "Fail"
    End If
    
    con.Close
    
End Sub
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
Solved. I modified the Module Code to the following one:
VBA Code:
Sub UserForm_Initialize()
    Load UserForm1
    UserForm1.Show
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,215,073
Messages
6,122,970
Members
449,095
Latest member
Mr Hughes

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