take the contents of a textbox and enter a character 4 positions from the end

Posted on 2005-03-14
Medium Priority
Last Modified: 2010-04-23
Hi Guys,

I've got a filepath which is in a textbox - G:\database.mdb
I am doing a 'compact database' function which takes the database path name from the contents of the textbox, compacts the database and then renames it.

I want it to rename it to the same as the original file, but with the addition of 'TC' at the end of the filename.
jro.CompactDatabase("Provider=Microsoft.Jet.OLEDB.4.0;Data Source= """ & textbox1.Text & """ ", _
        "Provider=Microsoft.Jet.OLEDB.4.0;Data Source= """ & textbox1.Text & "TC" & """ ;Jet OLEDB:Engine Type=5")
The code above works fine, except that it adds the 'TC' after the extention.
I.e: textbox1.text = G:\database.mdb   and the new file is called  G:\database.mdbTC
What I want is the file to be called  D:\databaseTC.mdb

I think it needs to count from the end backwards and then insert the 'TC' just before the 'dot'. (.mdb). This is because there will be different length filenames but they will always end in .mdb.

Any help would be great.

Many Thanks

Question by:matthewsampson
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 101

Expert Comment

ID: 13537135
Try something like

jro.CompactDatabase("Provider=Microsoft.Jet.OLEDB.4.0;Data Source= """ & textbox1.Text & """ ", _
        "Provider=Microsoft.Jet.OLEDB.4.0;Data Source= """ & left(textbox1.Text,length(textbox1.Text)-4) & "TC.mdb" & """ ;Jet OLEDB:Engine Type=5")


Accepted Solution

Joe_Griffith earned 2000 total points
ID: 13537138
I would use the Path class to get the parts of the filename like this:

    Dim temp As String
    Dim FileName As String
    Dim FileExt As String
    Dim NewName As String

    temp = TextBox1.Text

    FileName = Path.GetFileNameWithoutExtension(temp)
    FileExt = Path.GetExtension(temp)

    NewName = FileName & "TC." & FileExt

Expert Comment

ID: 13537169

if you want to split the name, use something like this:

strName = "test.mdb"
strNewName = strName.substring(0,strName.LastIndexOf(".") - 1) + "TC" + ".mdb"

Also, you can use the FileInfo object: it has a .name method which gives you the filename without extension. Get this name,
add "TC.mdb" and you're done.
Don't know if the syntax is exact, put a new comment if you can't get it right.

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Expert Comment

ID: 13537296
I'd strongly suggest using Joe_Griffith's suggestion, since it's a lot safer (if for some reason there's a platform change or whatever in the future), and also a lot easier to figure out just what's going on.

Author Comment

ID: 13537683
Hi Joe_Griffith,

I've been following your idea.
When using this bit of the code:
        FileName = Path.GetFileNameWithoutExtension(temp)
        FileExt = Path.GetExtension(temp)
It says that Path is not defined.
Did I miss something?

Expert Comment

ID: 13538157

Imports System.IO

or qualify Path as System.IO.Path...

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses
Course of the Month11 days, 20 hours left to enroll

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question