Click here to Skip to main content
15,885,767 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am not sure if I asked this correctly but bear with me while I explain.

I created a program, all the coding and everything is done, there is only one thing left for me to do and that's to make it so the user can save and load their values. The program is an EV Tracker for Pokemon: Dawn of Darkness. Form1 has

6 numupdowns (HPAmount, AtkAmount, DefAmount, SpAtkAmount, SpDefAmount, and SpdAmount)

3 buttons. The AddButton that takes the text from from AddPoke.Text and adds it to a combobox named PokeTeam and also creates a database record where the Pokemon value in the database is equal to AddPoke.Text. The Removebtn finds the combobox item from PokeTeam that is equal to AddPoke.Text and removes it, and also deletes the record from the database where the Pokemon value is equal to AddPoke.Text. And the third button is the Add EV button that increments the appropriate numupdown control based on what pokemon is selected in the Defeated combobox.

The Defeated combobox is populated by a third combobox named Training.

The PokeTeam combobox pulls the records from the database so that there is one item for each pokemon listed in the database. The numupdown controls are directly linked into the database so that the record for the pokemon selected in PokeTeam is updated everytime a value changes.

This is what I need to change. When I began this project I created an access database and linked it to the project. The database consists of one table named PokemonTable. The PokemonTable consists of 7 columns: Pokemon, HPValue, AtkValue, DefValue, SpAtkValue, SpDefValue, and SpdValue. I created the database using MS Access and the way the program is setup right now, it automatically loads the database when the program starts. I figured it would be easier to create a save code after everything was finished. What I didn't count on was the difficulty of learning how to change from a pre-created MS Access database to one that is created when the program starts.

I will detail all the coding I used when working with the current database. Can Someone explain to me, in detail and with replacement coding, how to convert my program from a pre-created database to a savable/loadable file.

Private Sub Form1_Load(ByVal sender As System.Object,
    ByVal e As System.EventArgs) Handles MyBase.Load

    PokeTeam.Items.Clear()

    DbConnection.ConnectionString = strConnectionString

    DbConnection.Open()

    Dim SqlQry As String = "SELECT * FROM PokemonTable"

    DbConnection = New OleDbConnection(strConnectionString)

    Try
        ' Open connection
        DbConnection.Open()

        'create data adapter
        Dim da As OleDbDataAdapter = New OleDbDataAdapter(SqlQry, DbConnection)

        'create dataset
        Dim ds As DataSet = New DataSet

        'fill dataset
        da.Fill(ds, "PokemonTable")

        'get data table
        Dim dt As DataTable = ds.Tables("PokemonTable")

        'display data
        Dim row As DataRow

        For Each row In dt.Rows
            PokeTeam.Items.Add(row("Pokemon"))
        Next row
    Catch ex As OleDbException
        MsgBox("Error: " & ex.ToString & vbCrLf)
    Finally
        ' Close connection
        DbConnection.Close()
    End Try
End Sub


VB
'This part is in the base form1 class

Dim DbConnection As New OleDbConnection

Dim DbCommand As New OleDbCommand

Dim DbInsert As New OleDbCommand

Dim DbUpdate As New OleDbCommand

Dim DbDelete As New OleDbCommand

Dim strConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" &
System.Environment.CurrentDirectory & "\EV Tracker.mdb"


VB
Private Sub AddButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AddButton.Click

    'Adds the text from AddPoke.Text to PokeTeam.Items
    If AddPoke.Text.Length > 3 Then
        PokeTeam.Items.Add(AddPoke.Text)
        DbInsert.CommandText = "INSERT INTO PokemonTable (Pokemon, HPValue, AtkValue, DefValue, SpAtkValue, SpDefValue, SpdValue) VALUES (@Pokemon, @HPValue, @AtkValue, @DefValue, @SpAtkValue, @SpDefValue, @SpdValue);"

        'Adds a record to the database with AddPoke.Text as the Pokemon column and gives 0 EV to each stat

        DbInsert.CommandType = CommandType.Text

        DbInsert.Connection = DbConnection

        DbInsert.Parameters.AddWithValue("@Pokemon", AddPoke.Text)
        DbInsert.Parameters.AddWithValue("@HPValue", 0)
        DbInsert.Parameters.AddWithValue("@AtkValue", 0)
        DbInsert.Parameters.AddWithValue("@DefValue", 0)
        DbInsert.Parameters.AddWithValue("@SpAtkValue", 0)
        DbInsert.Parameters.AddWithValue("@SpDefValue", 0)
        DbInsert.Parameters.AddWithValue("@SpdValue", 0)

        DbInsert.ExecuteNonQuery()
    End If

    DbInsert.Dispose()
    AddPoke.Text = ""

End Sub


I could go on giving more code but I believe if you help with these bits of code I can figure out the rest. I'll post a link below if you want to download all the files and see more of the code (I know I learned a lot from making this program so maybe some other newbie will be able to make use of it).Thank you in advance.

http://www.4shared.com/zip/EhaoiywUce/PDod_EV_Tracker_20.html[^]
Posted
Comments
CHill60 20-May-15 8:14am    
KitsunePhoenix 20-May-15 9:04am    
it went a long way to helping but there are three variables mentioned in the code it gives that it never declares, so I don't know what they are or what purpose they serve. Maybe you can help? CloudAppSettings, ConnSettings, and AccessCreateDB are the missing variables. And it mentions the code

CloudAccessDatabase.CreateNewDatabase(CloudAccessDatabase.DatabaseType.sample1)

but never says where it belongs, and when I put it into form1 it highlights CloudAccessDatabase and says a declaration is expected. This is why I posted the question, is because every tutorial I have found so far has had a problem like this where it gives most of the information but leaves out important bits that are impossible to figure out on your own.
CHill60 20-May-15 9:23am    
If you download the source zip file you will find CloudAccessDatabase.vb which contains the class CloudAccessDatabase - you need to change this to whichever class you create to manage your database
KitsunePhoenix 20-May-15 9:26am    
lol I didn't even see that download linking hiding at the top. Thank you
CHill60 20-May-15 9:28am    
:)

1 solution

Posting the link from my comments as the formal solution - Create Microsoft Access Database Programmatically using VB.NET[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900