VBA one form two separate databases

KristenLehman

New Member
Joined
Jun 24, 2014
Messages
7
Hello,

I created an excel form that contains drop downs, and fill in sections. It has 4 clickable buttons "Save", "Modify", "Delete", and "Reset"

I have two different sheets one called "Database 1", and the other called "Database 2".

Currently when I click on save everything gets dumped into Database 1.

I added a drop down called location, and it contains "Location 1", and "Location 2".

What I'm trying to do is if I select "Location 2" within the form and I push save I want everything to get dumped into "Database 2", and when I select Location 1, everything gets dumped into "Database 1".

This is my current code for "Save" how would I go about writing the code for if location = Location 1 dump data into Database 1 etc?

VBA Code:
Sub Save()

Dim frm As Worksheet
Dim database As Worksheet

Dim iRow As Long
Dim iSerial As Long

Set frm = ThisWorkbook.Sheets("Form")

Set database = ThisWorkbook.Sheets("Database 1")

If Trim(frm.Range("M1").Value) = "" Then

iRow = database.Range("A" & Application.Rows.Count).End(xlUp).Row + 1

If iRow = 2 Then
iSerial = 1

Else

iSerial = database.Cells(iRow - 1, 1).Value + 1

End If


Else

iRow = frm.Range("L1").Value
iSerial = frm.Range("M1").Value

End If

With database

.Cells(iRow, 1).Value = iSerial

.Cells(iRow, 2).Value = frm.Range("I11").Value
.Cells(iRow, 3).Value = frm.Range("I13").Value
.Cells(iRow, 4).Value = frm.Range("I15").Value
.Cells(iRow, 5).Value = frm.Range("I17").Value
.Cells(iRow, 6).Value = frm.Range("I19").Value
.Cells(iRow, 7).Value = frm.Range("I21").Value
.Cells(iRow, 8).Value = frm.Range("I23").Value
.Cells(iRow, 9).Value = frm.Range("I25").Value
.Cells(iRow, 10).Value = frm.Range("I27").Value
.Cells(iRow, 11).Value = frm.Range("I31").Value
.Cells(iRow, 12).Value = frm.Range("I33").Value
.Cells(iRow, 13).Value = frm.Range("I35").Value
.Cells(iRow, 14).Value = frm.Range("I7").Value
.Cells(iRow, 15).Value = [Text(Now(), "DD-MM-YYYY HH:MM:SS")]

End With

frm.Range("L1").Value = ""
frm.Range("M1").Value = ""
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
Hi,
with the following assumptions
- Form is a worksheet
- Save code is located in the Form worksheets code page
- Combobox is an ActiveX control

try following & see if does what you want

Rich (BB code):
Sub Save()
    
    Dim frm         As Worksheet
    Dim Database    As Worksheet
    
    Dim iRow        As Long
    Dim iSerial     As Long
    Dim Index       As Long
    
    Set frm = ThisWorkbook.Sheets("Form")
    
    
    Index = Me.Location.ListIndex + 1
    If Index = 0 Then Exit Sub
    
    Set Database = ThisWorkbook.Sheets("Database " & Index)

'rest of code

Dave
 
Upvote 0
Hello,

I created an excel form that contains drop downs, and fill in sections. It has 4 clickable buttons "Save", "Modify", "Delete", and "Reset"

I have two different sheets one called "Database 1", and the other called "Database 2".

Currently when I click on save everything gets dumped into Database 1.

I added a drop down called location, and it contains "Location 1", and "Location 2".

What I'm trying to do is if I select "Location 2" within the form and I push save I want everything to get dumped into "Database 2", and when I select Location 1, everything gets dumped into "Database 1".

This is my current code for "Save" how would I go about writing the code for if location = Location 1 dump data into Database 1 etc?

VBA Code:
Sub Save()

Dim frm As Worksheet
Dim database As Worksheet

Dim iRow As Long
Dim iSerial As Long

Set frm = ThisWorkbook.Sheets("Form")

Set database = ThisWorkbook.Sheets("Database 1")

If Trim(frm.Range("M1").Value) = "" Then

iRow = database.Range("A" & Application.Rows.Count).End(xlUp).Row + 1

If iRow = 2 Then
iSerial = 1

Else

iSerial = database.Cells(iRow - 1, 1).Value + 1

End If


Else

iRow = frm.Range("L1").Value
iSerial = frm.Range("M1").Value

End If

With database

.Cells(iRow, 1).Value = iSerial

.Cells(iRow, 2).Value = frm.Range("I11").Value
.Cells(iRow, 3).Value = frm.Range("I13").Value
.Cells(iRow, 4).Value = frm.Range("I15").Value
.Cells(iRow, 5).Value = frm.Range("I17").Value
.Cells(iRow, 6).Value = frm.Range("I19").Value
.Cells(iRow, 7).Value = frm.Range("I21").Value
.Cells(iRow, 8).Value = frm.Range("I23").Value
.Cells(iRow, 9).Value = frm.Range("I25").Value
.Cells(iRow, 10).Value = frm.Range("I27").Value
.Cells(iRow, 11).Value = frm.Range("I31").Value
.Cells(iRow, 12).Value = frm.Range("I33").Value
.Cells(iRow, 13).Value = frm.Range("I35").Value
.Cells(iRow, 14).Value = frm.Range("I7").Value
.Cells(iRow, 15).Value = [Text(Now(), "DD-MM-YYYY HH:MM:SS")]

End With

frm.Range("L1").Value = ""
frm.Range("M1").Value = ""
did it work ? i have the same "problem"
 
Upvote 0

Forum statistics

Threads
1,224,813
Messages
6,181,107
Members
453,021
Latest member
Justyna P

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